Skip to content

Commit 44ac5da

Browse files
committed
Refactor: Design Add and Search Words Data Structure
1 parent 853a332 commit 44ac5da

File tree

1 file changed

+5
-9
lines changed
  • design-add-and-search-words-data-structure

1 file changed

+5
-9
lines changed

design-add-and-search-words-data-structure/flynn.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,21 @@ class WordDictionary {
5656
TrieNode* root;
5757

5858
bool _search(string word, int idx, TrieNode* node) {
59+
if (node == nullptr) return false;
60+
5961
if (word.size() == idx) return node->word;
6062

6163
char c = word[idx];
6264

6365
if (c != '.') {
64-
if (node->links[c - 'a'] == nullptr) return false;
65-
6666
TrieNode* next_node = node->links[c - 'a'];
6767
int next_idx = idx + 1;
6868

6969
return _search(word, next_idx, next_node);
7070
} 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;
7874
}
7975
return false;
8076
}

0 commit comments

Comments
 (0)