# Why does my code has different result in IDE and LeetCode?

• The input is {"a","b"}, "a", "b". On my computer and in IDE, the result is "1", which is correct. But in the LeetCode System it is 2147483647... which indicate the result variable minLen has never been modified. Could anybody tell me why? It's too weird. Thanks!

``````public int shortestWordDistance(String[] words, String word1, String word2) {
int pos1 = -1;
int pos2 = -1;
int minLen = Integer.MAX_VALUE;

// if word1 and word2 are the same
if(word1.equals(word2)){
for(int i =0; i<words.length; i++){
if(words[i].equals(word1) ){
if(pos1<0){
pos1 = i;
}
if( pos1 >=0 && i-pos1>0 && i-pos1 <minLen ){
minLen = i-pos1;
pos1 = i;
}
}
}
return minLen;
}

//if word1 and word2 are different
for(int i=0; i<words.length; i++){
if(words[i] == word1 && words[i] != word2 ){
if(pos1<0){ //meet first word1
pos1 = i;
}

if( pos2>=0 && i-pos2 < minLen){ //have met word2 and the distance is shorter
minLen = i-pos2;
}
}
else if (words[i] == word2 && words[i] != word1){
if(pos2<0){ //meet first word2
pos2 = i;
}
if(pos1>=0 &&  i-pos1< minLen){ //have met word1 and the distance is shorter
minLen = i-pos1;
}
}
}
return minLen;
}``````

• Ahhhh....This is stupid...
For these two lines:

`````` if(words[i] == word1 && words[i] != word2 )
``````

and

``````else if (words[i] == word2 && words[i] != word1){
``````

Apparently in Java the conditions can be true only if word[i] and word1 point to same object. I guess there's some optimization in Eclipse that makes it so. The right way to right it should be:

`````` words[i].equals(word1)
``````

Also there's bug in the code. So don't take it as an answer!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.