-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdesign-add-and-search-words-data-structure.js
59 lines (49 loc) · 1.51 KB
/
design-add-and-search-words-data-structure.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
/**Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary class:
WordDictionary() Initializes the object.
void addWord(word) Adds word to the data structure, it can be matched later.
bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter. */
class TrieNode {
constructor() {
this.childrens = {};
this.endW = false;
}
}
class WordDictionary {
constructor() {
this.root = new TrieNode();
}
addWord(word) {
let curr = this.root;
for (let ch of word) {
if (!(ch in curr.childrens)) {
curr.childrens[ch] = new TrieNode();
}
curr = curr.childrens[ch];
}
curr.endW = true;
}
search(word) {
function dfs(node, idx) {
let curr = node;
for (let i = idx; i < word.length; i++) {
let ch = word[i];
if (ch === ".") {
for (let child of Object.values(curr.childrens)) {
if (dfs(child, i + 1)) {
return true;
}
}
return false;
} else {
if (!(ch in curr.childrens)) {
return false;
}
curr = curr.childrens[ch];
}
}
return curr.endW;
}
return dfs(this.root, 0);
}
};