05 August 2008
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with different parameters.
Example:
Assume that words =
.1
["practice", "makes", "perfect", "coding", "makes"]
1
2
Input: word1 = “coding”, word2 = “practice”
Output: 3
1
2
Input: word1 = "makes", word2 = "coding"
Output: 1
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
字典中有重复的单词,根据传进来的参数,需要找到两个字符串的位置最近的距离,在I中那道题243. Shortest Word Distance中,可以用直接计算然后选出并返回最短的距离即可,这道题用同样的思路会超时;用hashmap来进行,预处理,然后找到两个单词之间的最近距离。
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
class WordDistance {
private Map<String, List<Integer>> map;
public WordDistance(String[] words) {
// 构造器中先把单词(可能有重复)的各自下标进行预处理
map = new HashMap<String, List<Integer>>();
for(int i = 0; i < words.length; i++) {
String word = words[i];
if(map.containsKey(word)) {
map.get(word).add(i);
} else {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
map.put(word, list);
}
}
}
public int shortest(String word1, String word2) {
// 对比两个list之间各元素的最小差值
List<Integer> list1 = map.get(word1);
List<Integer> list2 = map.get(word2);
int distance = Integer.MAX_VALUE;
for(int i = 0, j = 0; i < list1.size() && j < list2.size(); ) {
int index1 = list1.get(i), index2 = list2.get(j);
//distance = Math.min(distance, Math.abs(index1 - index2));
if(index1 < index2) {
distance = Math.min(distance, index2 - index1);
i++;
} else {
distance = Math.min(distance, index1 - index2);
j++;
}
}
return distance;
}
}
/**
* Your WordDistance object will be instantiated and called as such:
* WordDistance obj = new WordDistance(words);
* int param_1 = obj.shortest(word1,word2);
*/