-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0208. Implement Trie (Prefix Tree).js
63 lines (58 loc) · 1.66 KB
/
0208. Implement Trie (Prefix Tree).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
// Implement a trie with insert, search, and startsWith methods.
//
// Note:
// You may assume that all inputs are consist of lowercase letters a-z.
// Space O(M*N*K)
// If we have M words to insert in total and the length of words is at most N, there will be at most M*N nodes in the
// worst case (any two words don't have a common prefix).
// Let's assume that there are maximum K different characters (K is equal to 26 in this problem, but might differs
// in different cases). So each node will maintain a map whose size is at most K.
class Trie {
constructor() {
this.root = {};
}
/**
* Inserts a word into the trie.
* @param {string} word
* @return {void}
*/
// Time O(N), N is the longest length of the word
insert(word) {
let node = this.root;
// word.split('').forEach(c => node = node[c] = node[c] || {});
// word.split('').forEach(c => node = (node[c] ? node[c] : node[c] = {}));
for (const c of word) {
if (node[c] == null) node[c] = {};
node = node[c];
}
node.isWord = true;
}
traverse(word) {
let node = this.root;
for (const c of word) {
node = node[c];
if (node == null) return null;
}
return node;
}
/**
* Returns if the word is in the trie.
* @param {string} word
* @return {boolean}
*/
// Time O(N)
search(word) {
const node = this.traverse(word);
return node != null && node.isWord === true;
}
/**
* Returns if there is any word in the trie that starts with the given prefix.
* @param {string} prefix
* @return {boolean}
*/
// Time O(N)
startsWith(prefix) {
const node = this.traverse(prefix);
return node != null;
}
}