File tree Expand file tree Collapse file tree 1 file changed +5
-9
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change @@ -56,25 +56,21 @@ class WordDictionary {
56
56
TrieNode* root;
57
57
58
58
bool _search (string word, int idx, TrieNode* node) {
59
+ if (node == nullptr ) return false ;
60
+
59
61
if (word.size () == idx) return node->word ;
60
62
61
63
char c = word[idx];
62
64
63
65
if (c != ' .' ) {
64
- if (node->links [c - ' a' ] == nullptr ) return false ;
65
-
66
66
TrieNode* next_node = node->links [c - ' a' ];
67
67
int next_idx = idx + 1 ;
68
68
69
69
return _search (word, next_idx, next_node);
70
70
} else {
71
- for (TrieNode* link : node->links ) {
72
- if (link != nullptr ) {
73
- TrieNode* next_node = link;
74
- int next_idx = idx + 1 ;
75
-
76
- if (_search (word, next_idx, next_node)) return true ;
77
- }
71
+ for (TrieNode* next_node : node->links ) {
72
+ int next_idx = idx + 1 ;
73
+ if (_search (word, next_idx, next_node)) return true ;
78
74
}
79
75
return false ;
80
76
}
You can’t perform that action at this time.
0 commit comments