File tree Expand file tree Collapse file tree 2 files changed +7
-1
lines changed Expand file tree Collapse file tree 2 files changed +7
-1
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,10 @@ export default class TrieNode {
29
29
this . children . set ( character , new TrieNode ( character , isCompleteWord ) ) ;
30
30
}
31
31
32
- return this . children . get ( character ) ;
32
+ const childNode = this . children . get ( character ) ;
33
+ childNode . isCompleteWord = childNode . isCompleteWord || isCompleteWord ;
34
+
35
+ return childNode ;
33
36
}
34
37
35
38
/**
Original file line number Diff line number Diff line change @@ -41,11 +41,14 @@ describe('Trie', () => {
41
41
42
42
trie . addWord ( 'cat' ) ;
43
43
trie . addWord ( 'cats' ) ;
44
+ trie . addWord ( 'carpet' ) ;
44
45
trie . addWord ( 'car' ) ;
45
46
trie . addWord ( 'caption' ) ;
46
47
47
48
expect ( trie . doesWordExist ( 'cat' ) ) . toBe ( true ) ;
48
49
expect ( trie . doesWordExist ( 'cats' ) ) . toBe ( true ) ;
50
+ expect ( trie . doesWordExist ( 'carpet' ) ) . toBe ( true ) ;
51
+ expect ( trie . doesWordExist ( 'car' ) ) . toBe ( true ) ;
49
52
expect ( trie . doesWordExist ( 'cap' ) ) . toBe ( false ) ;
50
53
expect ( trie . doesWordExist ( 'call' ) ) . toBe ( false ) ;
51
54
} ) ;
You can’t perform that action at this time.
0 commit comments