-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path244.cpp
More file actions
30 lines (28 loc) · 880 Bytes
/
244.cpp
File metadata and controls
30 lines (28 loc) · 880 Bytes
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
class WordDistance {
public:
unordered_map<string, vector<int>> mp;
WordDistance(vector<string>& wordsDict) {
for (int i = 0; i < wordsDict.size(); ++i) {
mp[wordsDict[i]].push_back(i);
}
}
int shortest(string word1, string word2) {
const vector<int>& v1 = mp[word1];
const vector<int>& v2 = mp[word2];
int res = INT_MAX;
int idx1 = 0;
int idx2 = 0;
while (idx1 < v1.size() && idx2 < v2.size()) {
res = min(res, abs(v1[idx1] - v2[idx2]));
if (v1[idx1] > v2[idx2]) idx2++;
else if (v1[idx1] < v2[idx2]) idx1++;
else break;
}
return res;
}
};
/**
* Your WordDistance object will be instantiated and called as such:
* WordDistance* obj = new WordDistance(wordsDict);
* int param_1 = obj->shortest(word1,word2);
*/