class trie { public: vector<trie*> children; bool is_end; trie() { children = vector<trie*>(26, nullptr); is_end = false; } void insert(const string& word) { trie* cur = this; for (char c : word) { c -= 'a'; if (cur->children[c] == nullptr) { cur->children[c] = new trie; } cur = cur->children[c]; } cur->is_end = true; } }; class WordDictionary { private: trie* root; public: WordDictionary() : root(new trie) {} void addWord(string word) { root->insert(word); } bool search(string word) { return dfs(word, 0, root); } private: bool dfs(const string& word, int i, trie* cur) { if (i == word.size()) { return cur->is_end; } char c = word[i]; if (c != '.') { trie* child = cur->children[c - 'a']; if (child != nullptr && dfs(word, i + 1, child)) { return true; } } else { for (trie* child : cur->children) { if (child != nullptr && dfs(word, i + 1, child)) { return true; } } } return false; } }; /** * Your WordDictionary object will be instantiated and called as such: * WordDictionary* obj = new WordDictionary(); * obj->addWord(word); * bool param_2 = obj->search(word); */