class Solution {
public int shortestDistance(String[] wordsDict, String word1, String word2) {
int index1 = -1;//word1最后出现的位置
int index2 = -1;//word2最后出现的位置
int minDistance = Integer.MAX_VALUE;//最短距离
for(int i = 0; i < wordsDict.length;i++){
if(wordsDict[i].equals(word1)){
index1 = i;
if(index2 != -1)
minDistance = Math.min(minDistance,index1-index2);
}
if(wordsDict[i].equals(word2)){
index2 = i;
if(index1 != -1)
minDistance = Math.min(minDistance,index2-index1);
}
}
return minDistance;
}
}