Plz correct this
here is the correct logic, please implement this: (note this is pseudocode, dont blindly copy paste)
int shortestDistance(String[] words, String word1, String word2) {
int m=-1;
int n=-1;
int min = Maximum integer value possible;
for(int i=0; i<words.length; i++){
String s = words[i];
if(word1.equals(s)){
m = i;
if(n!=-1)
min = Minimum(min, m-n);
}else if(word2.equals(s)){
n = i;
if(m!=-1)
min = Minimum(min, n-m);
}
}
return min;
}
We are doing 2-pointer approach, one pointing to word1 and other pointing to word2 and keeping the minimum difference between the pointers as our answer
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.