forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path244_WordDistance.java
78 lines (69 loc) · 2.54 KB
/
244_WordDistance.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//优化一下shortest,因为都是sorted的组合,所以merge以下,找不同,O(m+n)
public class WordDistance {
HashMap<String, ArrayList<Integer>> map;
public WordDistance(String[] words) {
map = new HashMap<String, ArrayList<Integer>>();
for(int i = 0; i < words.length; i++){
String word = words[i];
ArrayList<Integer> list = new ArrayList<Integer>();
if(map.containsKey(word)){
list = map.get(word);
}
list.add(i);
map.put(word, list);
}
}
public int shortest(String word1, String word2) {
ArrayList<Integer> list1 = map.get(word1);
ArrayList<Integer> list2 = map.get(word2);
int res = Integer.MAX_VALUE;
for(int i = 0, j = 0; i < list1.size() && j < list2.size(); ){ // pay attention to the condition here!!!!
int first = list1.get(i);
int second = list2.get(j);
if(first < second){
res = Math.min(res, second-first);
i++;
}
else if(first > second){
res = Math.min(res, first-second);
j++;
}
}
return res;
}
}
// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");
//Naive解法。 shortest的复杂度是O(m*n)
public class WordDistance {
HashMap<String, ArrayList<Integer>> map;
public WordDistance(String[] words) {
map = new HashMap<String, ArrayList<Integer>>();
for(int i = 0; i < words.length; i++){
String word = words[i];
ArrayList<Integer> list = new ArrayList<Integer>();
if(map.containsKey(word)){
list = map.get(word);
}
list.add(i);
map.put(word, list);
}
}
public int shortest(String word1, String word2) {
ArrayList<Integer> list1 = map.get(word1);
ArrayList<Integer> list2 = map.get(word2);
int res = Integer.MAX_VALUE;
for(int i: list1){
for(int j : list2){
res = Math.min(res, Math.abs(i-j));
}
}
return res;
}
}
// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");