Skip to content

Commit bdc1be7

Browse files
committed
feat: add solutions to leetcode problem: No.0244. Shortest Word Distance II
1 parent e4f9741 commit bdc1be7

File tree

4 files changed

+166
-22
lines changed

4 files changed

+166
-22
lines changed

solution/0200-0299/0244.Shortest Word Distance II/README.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<p><strong>注意:</strong><br>
2222
你可以假设 <em>word1</em> <strong>不等于</strong> <em>word2</em>, 并且 <em>word1</em> 和 <em>word2</em> 都在列表里。</p>
2323

24-
2524
## 解法
2625

2726
<!-- 这里可写通用的实现逻辑 -->
@@ -33,15 +32,73 @@
3332
<!-- 这里可写当前语言的特殊实现逻辑 -->
3433

3534
```python
36-
35+
class WordDistance:
36+
37+
def __init__(self, wordsDict: List[str]):
38+
self.words = {}
39+
for i, word in enumerate(wordsDict):
40+
indexes = self.words.get(word, [])
41+
indexes.append(i)
42+
self.words[word] = indexes
43+
44+
def shortest(self, word1: str, word2: str) -> int:
45+
idx1, idx2 = self.words[word1], self.words[word2]
46+
i1 = i2 = 0
47+
shortest = float('inf')
48+
while i1 < len(idx1) and i2 < len(idx2):
49+
shortest = min(shortest, abs(idx1[i1] - idx2[i2]))
50+
smaller = idx1[i1] < idx2[i2]
51+
if smaller:
52+
i1 += 1
53+
else:
54+
i2 += 1
55+
return shortest
56+
57+
58+
# Your WordDistance object will be instantiated and called as such:
59+
# obj = WordDistance(wordsDict)
60+
# param_1 = obj.shortest(word1,word2)
3761
```
3862

3963
### **Java**
4064

4165
<!-- 这里可写当前语言的特殊实现逻辑 -->
4266

4367
```java
44-
68+
class WordDistance {
69+
private Map<String, List<Integer>> words;
70+
71+
public WordDistance(String[] wordsDict) {
72+
words = new HashMap<>();
73+
for (int i = 0; i < wordsDict.length; ++i) {
74+
List<Integer> indexes = words.getOrDefault(wordsDict[i], new ArrayList<>());
75+
indexes.add(i);
76+
words.put(wordsDict[i], indexes);
77+
}
78+
}
79+
80+
public int shortest(String word1, String word2) {
81+
List<Integer> idx1 = words.get(word1);
82+
List<Integer> idx2 = words.get(word2);
83+
int i1 = 0, i2 = 0, shortest = Integer.MAX_VALUE;
84+
while (i1 < idx1.size() && i2 < idx2.size()) {
85+
shortest = Math.min(shortest, Math.abs(idx1.get(i1) - idx2.get(i2)));
86+
boolean smaller = idx1.get(i1) < idx2.get(i2);
87+
if (smaller) {
88+
++i1;
89+
} else {
90+
++i2;
91+
}
92+
}
93+
return shortest;
94+
}
95+
}
96+
97+
/**
98+
* Your WordDistance object will be instantiated and called as such:
99+
* WordDistance obj = new WordDistance(wordsDict);
100+
* int param_1 = obj.shortest(word1,word2);
101+
*/
45102
```
46103

47104
### **...**

solution/0200-0299/0244.Shortest Word Distance II/README_EN.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,78 @@ wordDistance.shortest(&quot;makes&quot;, &quot;coding&quot;); // return 1
4141
<li>At most <code>5000</code> calls will be made to <code>shortest</code>.</li>
4242
</ul>
4343

44-
4544
## Solutions
4645

4746
<!-- tabs:start -->
4847

4948
### **Python3**
5049

5150
```python
52-
51+
class WordDistance:
52+
53+
def __init__(self, wordsDict: List[str]):
54+
self.words = {}
55+
for i, word in enumerate(wordsDict):
56+
indexes = self.words.get(word, [])
57+
indexes.append(i)
58+
self.words[word] = indexes
59+
60+
def shortest(self, word1: str, word2: str) -> int:
61+
idx1, idx2 = self.words[word1], self.words[word2]
62+
i1 = i2 = 0
63+
shortest = float('inf')
64+
while i1 < len(idx1) and i2 < len(idx2):
65+
shortest = min(shortest, abs(idx1[i1] - idx2[i2]))
66+
smaller = idx1[i1] < idx2[i2]
67+
if smaller:
68+
i1 += 1
69+
else:
70+
i2 += 1
71+
return shortest
72+
73+
74+
# Your WordDistance object will be instantiated and called as such:
75+
# obj = WordDistance(wordsDict)
76+
# param_1 = obj.shortest(word1,word2)
5377
```
5478

5579
### **Java**
5680

5781
```java
58-
82+
class WordDistance {
83+
private Map<String, List<Integer>> words;
84+
85+
public WordDistance(String[] wordsDict) {
86+
words = new HashMap<>();
87+
for (int i = 0; i < wordsDict.length; ++i) {
88+
List<Integer> indexes = words.getOrDefault(wordsDict[i], new ArrayList<>());
89+
indexes.add(i);
90+
words.put(wordsDict[i], indexes);
91+
}
92+
}
93+
94+
public int shortest(String word1, String word2) {
95+
List<Integer> idx1 = words.get(word1);
96+
List<Integer> idx2 = words.get(word2);
97+
int i1 = 0, i2 = 0, shortest = Integer.MAX_VALUE;
98+
while (i1 < idx1.size() && i2 < idx2.size()) {
99+
shortest = Math.min(shortest, Math.abs(idx1.get(i1) - idx2.get(i2)));
100+
boolean smaller = idx1.get(i1) < idx2.get(i2);
101+
if (smaller) {
102+
++i1;
103+
} else {
104+
++i2;
105+
}
106+
}
107+
return shortest;
108+
}
109+
}
110+
111+
/**
112+
* Your WordDistance object will be instantiated and called as such:
113+
* WordDistance obj = new WordDistance(wordsDict);
114+
* int param_1 = obj.shortest(word1,word2);
115+
*/
59116
```
60117

61118
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
class WordDistance {
2+
private Map<String, List<Integer>> words;
23

3-
Map<String, List<Integer>> map = new HashMap<>();
4-
5-
public WordDistance(String[] words) {
6-
for (int i = 0; i < words.length; i++) {
7-
List<Integer> indexList = map.get(words[i]) == null ? new ArrayList<>() : map.get(words[i]);
8-
indexList.add(i);
9-
map.put(words[i], indexList);
4+
public WordDistance(String[] wordsDict) {
5+
words = new HashMap<>();
6+
for (int i = 0; i < wordsDict.length; ++i) {
7+
List<Integer> indexes = words.getOrDefault(wordsDict[i], new ArrayList<>());
8+
indexes.add(i);
9+
words.put(wordsDict[i], indexes);
1010
}
1111
}
12-
12+
1313
public int shortest(String word1, String word2) {
14-
List<Integer> list1 = map.get(word1);
15-
List<Integer> list2 = map.get(word2);
16-
int ans = Integer.MAX_VALUE;
17-
for (int l1 : list1) {
18-
for (int l2 : list2) {
19-
ans = Math.min(ans, Math.abs(l1 - l2));
14+
List<Integer> idx1 = words.get(word1);
15+
List<Integer> idx2 = words.get(word2);
16+
int i1 = 0, i2 = 0, shortest = Integer.MAX_VALUE;
17+
while (i1 < idx1.size() && i2 < idx2.size()) {
18+
shortest = Math.min(shortest, Math.abs(idx1.get(i1) - idx2.get(i2)));
19+
boolean smaller = idx1.get(i1) < idx2.get(i2);
20+
if (smaller) {
21+
++i1;
22+
} else {
23+
++i2;
2024
}
2125
}
22-
return ans;
26+
return shortest;
2327
}
2428
}
2529

2630
/**
2731
* Your WordDistance object will be instantiated and called as such:
28-
* WordDistance obj = new WordDistance(words);
32+
* WordDistance obj = new WordDistance(wordsDict);
2933
* int param_1 = obj.shortest(word1,word2);
3034
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class WordDistance:
2+
3+
def __init__(self, wordsDict: List[str]):
4+
self.words = {}
5+
for i, word in enumerate(wordsDict):
6+
indexes = self.words.get(word, [])
7+
indexes.append(i)
8+
self.words[word] = indexes
9+
10+
def shortest(self, word1: str, word2: str) -> int:
11+
idx1, idx2 = self.words[word1], self.words[word2]
12+
i1 = i2 = 0
13+
shortest = float('inf')
14+
while i1 < len(idx1) and i2 < len(idx2):
15+
shortest = min(shortest, abs(idx1[i1] - idx2[i2]))
16+
smaller = idx1[i1] < idx2[i2]
17+
if smaller:
18+
i1 += 1
19+
else:
20+
i2 += 1
21+
return shortest
22+
23+
24+
# Your WordDistance object will be instantiated and called as such:
25+
# obj = WordDistance(wordsDict)
26+
# param_1 = obj.shortest(word1,word2)

0 commit comments

Comments
 (0)