```
public int shortestDistance(String[] words, String word1, String word2) {
int first=0, foundNum = 0, minValue=Integer.MAX_VALUE;
for(int i=0; i<words.length; i++) {
if(words[i].equals(word1)) {
if(foundNum == 2) {
minValue = Math.min(i-first, minValue);
}
foundNum = 1;
first = i;
continue;
}
if(words[i].equals(word2)) {
if(foundNum == 1) {
minValue = Math.min(i-first, minValue);
}
foundNum = 2;
first = i;
continue;
}
}
return minValue;
}
```