/** Initialize your data structure here. */ Trie():child(26),isEnd(0) {}
/** Inserts a word into the trie. */ voidinsert(string word){ Trie* node = this; for(char c : word){ int pos = c - 'a'; if(node->child[pos] == nullptr){ node->child[pos] = newTrie(); } node = node->child[pos]; } node->isEnd = 1; }
/** Returns if the word is in the trie. */ boolsearch(string word){ Trie* node = this; for(char c : word){ int pos = c - 'a'; if(node->child[pos] == nullptr){ node = node->child[pos]; break; } node = node->child[pos]; } if(node != nullptr && node->isEnd == 1)return1; return0; }
/** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Trie* node = this; for(char c : prefix){ int pos = c - 'a'; if(node->child[pos] == nullptr){ node = node->child[pos]; break; } node = node->child[pos]; } if(node != nullptr)return1; return0; } };
/** Initialize your data structure here. */ Trie():child(26),isEnd(0) {}
/** Inserts a word into the trie. */ voidinsert(string word){ Trie* node = this; for(char c : word){ int pos = c - 'a'; if(node->child[pos] == nullptr){ node->child[pos] = newTrie(); } node = node->child[pos]; } node->isEnd = 1; }
/** Returns if the word is in the trie. */ boolsearch(string word){ Trie* node = search_prefix(word); if(node != nullptr && node->isEnd == 1)return1; return0; }
/** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Trie* node = search_prefix(prefix); if(node != nullptr)return1; return0; } };