-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode244.java
35 lines (30 loc) · 1.06 KB
/
leetcode244.java
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
public class WordDistance {
Map<String, List<Integer>> map = new HashMap<>();
public WordDistance(String[] words) {
for(int i = 0; i < words.length; i++){
if(map.containsKey(words[i])){
map.get(words[i]).add(i);
}
else{
List<Integer> list = new ArrayList<>();
list.add(i);
map.put(words[i],list);
}
}
}
public int shortest(String word1, String word2) {
List<Integer> list1 = map.get(word1);
List<Integer> list2 = map.get(word2);
int min = Integer.MAX_VALUE;
for(int i = 0; i < list1.size(); i++){
for(int j = 0; j < list2.size(); j++){
min = Math.min(min, Math.abs(list1.get(i) - list2.get(j)));
}
}
return min;
}
}
// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");