-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_245.java
More file actions
28 lines (26 loc) · 837 Bytes
/
Copy pathP_245.java
File metadata and controls
28 lines (26 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package leetcode.medium;
public class P_245 {
public int shortestWordDistance(String[] words, String word1, String word2) {
boolean turn = true;
int w1 = -1, w2 = -1, res = words.length;
for (int i = 0; i < words.length; i++) {
if (word1.equals(word2)) {
if (words[i].equals(word1)) {
if (turn) {
w1 = i;
} else {
w2 = i;
}
turn ^= true;
}
} else {
if (words[i].equals(word1)) { w1 = i; }
if (words[i].equals(word2)) { w2 = i; }
}
if (w1 != -1 && w2 != -1) {
res = Math.min(res, Math.abs(w1 - w2));
}
}
return res;
}
}