forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest-word-distance-ii.js
65 lines (56 loc) · 1.44 KB
/
shortest-word-distance-ii.js
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
/**
* Shortest Word Distance II
*
* 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 = ["practice", "makes", "perfect", "coding", "makes"].
*
* Input: word1 = “coding”, word2 = “practice”
* Output: 3
*
* 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.
*/
class WordDistance {
/**
* @param {string[]} words
*/
constructor(words) {
this.map = {};
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (!(word in this.map)) {
this.map[word] = [];
}
this.map[word].push(i);
}
}
/**
* @param {string} word1
* @param {string} word2
* @return {number}
*/
shortest(word1, word2) {
const list1 = this.map[word1];
const list2 = this.map[word2];
let min = Infinity;
for (let i = 0, j = 0; i < list1.length && j < list2.length; ) {
const index1 = list1[i];
const index2 = list2[j];
min = Math.min(min, Math.abs(index1 - index2));
if (index1 < index2) {
i++;
} else {
j++;
}
}
return min;
}
}
export { WordDistance };