Skip to content

Commit d3ae2ba

Browse files
refactor 244
1 parent 371f407 commit d3ae2ba

File tree

1 file changed

+34
-33
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+34
-33
lines changed

src/main/java/com/fishercoder/solutions/_244.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import java.util.Map;
77

88
/**
9+
* 244. Shortest Word Distance II
10+
*
911
* This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
10-
11-
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.
12+
* 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.
1213
1314
For example,
1415
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
@@ -20,39 +21,39 @@
2021
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
2122
*/
2223
public class _244 {
23-
24-
private Map<String, List<Integer>> map;
25-
26-
public _244(String[] words) {
27-
map = new HashMap<>();
28-
for (int i = 0; i < words.length; i++) {
29-
String w = words[i];
30-
if (map.containsKey(w)) {
31-
map.get(w).add(i);
32-
} else {
33-
List<Integer> list = new ArrayList<>();
34-
list.add(i);
35-
map.put(w, list);
24+
public static class Solution1 {
25+
class WordDistance {
26+
private Map<String, List<Integer>> map;
27+
public WordDistance(String[] words) {
28+
map = new HashMap<>();
29+
for (int i = 0; i < words.length; i++) {
30+
String w = words[i];
31+
if (map.containsKey(w)) {
32+
map.get(w).add(i);
33+
} else {
34+
List<Integer> list = new ArrayList<>();
35+
list.add(i);
36+
map.put(w, list);
37+
}
38+
}
3639
}
37-
}
38-
}
39-
40-
public int shortest(String word1, String word2) {
41-
List<Integer> list1 = map.get(word1);
42-
List<Integer> list2 = map.get(word2);
43-
int result = Integer.MAX_VALUE;
44-
for (int i = 0, j = 0; i < list1.size() && j < list2.size(); ) {
45-
int index1 = list1.get(i);
46-
int index2 = list2.get(j);
47-
if (index1 < index2) {
48-
result = Math.min(result, index2 - index1);
49-
i++;
50-
} else {
51-
result = Math.min(result, index1 - index2);
52-
j++;
40+
public int shortest(String word1, String word2) {
41+
List<Integer> list1 = map.get(word1);
42+
List<Integer> list2 = map.get(word2);
43+
int result = Integer.MAX_VALUE;
44+
for (int i = 0, j = 0; i < list1.size() && j < list2.size(); ) {
45+
int index1 = list1.get(i);
46+
int index2 = list2.get(j);
47+
if (index1 < index2) {
48+
result = Math.min(result, index2 - index1);
49+
i++;
50+
} else {
51+
result = Math.min(result, index1 - index2);
52+
j++;
53+
}
54+
}
55+
return result;
5356
}
5457
}
55-
return result;
5658
}
57-
5859
}

0 commit comments

Comments
 (0)