Skip to content

Commit 3e4ebe1

Browse files
committed
desing add and search words data structure solution
1 parent 25a75aa commit 3e4ebe1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const TrieNode = function () {
2+
this.children = {};
3+
this.isEndOfWord = false;
4+
};
5+
var WordDictionary = function () {
6+
this.root = new TrieNode();
7+
};
8+
9+
/**
10+
* @param {string} word
11+
* @return {void}
12+
*/
13+
WordDictionary.prototype.addWord = function (word) {
14+
let currentNode = this.root;
15+
for (let i = 0; i < word.length; i++) {
16+
let char = word[i];
17+
if (!currentNode.children[char]) {
18+
currentNode.children[char] = new TrieNode();
19+
}
20+
currentNode = currentNode.children[char];
21+
}
22+
currentNode.isEndOfWord = true;
23+
};
24+
25+
/**
26+
* @param {string} word
27+
* @return {boolean}
28+
*/
29+
WordDictionary.prototype.search = function (word) {};
30+
31+
/**
32+
* Your WordDictionary object will be instantiated and called as such:
33+
* var obj = new WordDictionary()
34+
* obj.addWord(word)
35+
* var param_2 = obj.search(word)
36+
*/

0 commit comments

Comments
 (0)