-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_244.java
More file actions
37 lines (31 loc) · 1.04 KB
/
Copy pathP_244.java
File metadata and controls
37 lines (31 loc) · 1.04 KB
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
29
30
31
32
33
34
35
36
37
package leetcode.medium;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("unused")
public class P_244 {
static class WordDistance {
Map<String, List<Integer>> indexes;
WordDistance(String[] words) {
indexes = new HashMap<>();
for (int i = 0; i < words.length; i++) {
indexes.computeIfAbsent(words[i], v -> new ArrayList<>()).add(i);
}
}
public int shortest(String word1, String word2) {
final List<Integer> loc1 = indexes.get(word1);
final List<Integer> loc2 = indexes.get(word2);
int l1 = 0, l2 = 0, res = Integer.MAX_VALUE;
while (l1 < loc1.size() && l2 < loc2.size()) {
res = Math.min(res, Math.abs(loc1.get(l1) - loc2.get(l2)));
if (loc1.get(l1) < loc2.get(l2)) {
l1++;
} else {
l2++;
}
}
return res;
}
}
}