-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0211-design-add-and-search-words-data-structure.js
60 lines (52 loc) · 1.41 KB
/
0211-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
60
/**
* 211. Design Add and Search Words Data Structure
* https://leetcode.com/problems/design-add-and-search-words-data-structure/
* Difficulty: Medium
*
* 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.
*/
var WordDictionary = function() {
this.root = {};
};
/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word) {
let node = this.root;
for (const character of word) {
node[character] = node[character] || {};
node = node[character];
}
node.isTail = true;
};
/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word) {
return dfs(this.root, 0);
function dfs(node, i) {
if (word.length === i) {
return node.isTail;
}
if (word[i] === '.') {
for (const key in node) {
if (dfs(node[key], i + 1)) {
return true;
}
}
} else if (node[word[i]] && dfs(node[word[i]], i + 1)) {
return true;
}
return false;
}
};