shortest indices must be next to each other
40.87 %
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
if (words == null || words.length < 2) {
return -1;
}
int p1 = -1;
int p2 = -1;
int min = words.length;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
p1 = i;
}
if (words[i].equals(word2)) {
p2 = i;
}
if (p1 != -1 && p2 != -1) {
min = Math.min(min, Math.abs(p1 - p2));
}
}
return min;
}
}
No comments:
Post a Comment