File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments