classTrie { public: /** Initialize your data structure here. */ Trie() :root{new TrieNode{}} {} ~Trie(){ delete root; } /** Inserts a word into the trie. */ voidinsert(string word){ TrieNode* p = root; for(auto c : word){ auto i = static_cast<size_t>(c - 'a'); if(p->children[i] == nullptr) p->children[i] = new TrieNode{}; p = p->children[i]; } p->isWord = true; } /** Returns if the word is in the trie. */ boolsearch(string word){ TrieNode* p = root; for(auto c : word){ p = p->children[c - 'a']; if(!p) returnfalse; } if(p->isWord) returntrue; elsereturnfalse; } /** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ TrieNode* p = root; for(auto c : prefix){ p = p->children[c - 'a']; if(!p) returnfalse; } returntrue; } private: classTrieNode{ public: bool isWord; array<TrieNode*, 26> children; TrieNode() :isWord{false} ,children{nullptr} {} ~TrieNode(){ for(auto ptr : children){ delete ptr; } } }; TrieNode* root; };
classTrie { classTrieNode; using TrieNodePtr = shared_ptr<TrieNode>; public: /** Initialize your data structure here. */ Trie() :root{} { root = make_shared<TrieNode>(); }
/** Inserts a word into the trie. */ voidinsert(string word){ TrieNodePtr p = root; for(auto c : word){ auto i = static_cast<size_t>(c - 'a'); if(p->children[i] == nullptr) p->children[i] = make_unique<TrieNode>(); p = p->children[i]; } p->isWord = true; } /** Returns if the word is in the trie. */ boolsearch(string word){ TrieNodePtr p = root; for(auto c : word){ p = p->children[c - 'a']; if(!p) returnfalse; } if(p->isWord) returntrue; elsereturnfalse; } /** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ TrieNodePtr p = root; for(auto c : prefix){ p = p->children[c - 'a']; if(!p) returnfalse; } returntrue; } private: classTrieNode{ public: TrieNode() :isWord{false} ,children{nullptr} {} bool isWord; array<TrieNodePtr, 26> children; }; TrieNodePtr root; };
classWordDictionary { classnode; public: /** Initialize your data structure here. */ WordDictionary() :root{new node{}} {} ~WordDictionary(){ delete root; } /** Adds a word into the data structure. */ voidaddWord(string word){ auto p = root; for(auto c : word){ auto i = static_cast<size_t>(c - 'a'); if(!p->children[i]) p->children[i] = new node{}; p = p->children[i]; } p->isWord = true; } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ boolsearch(string word){ returnsearchImpl(root, word, word.begin()); } private: node* root; template<typename It> boolsearchImpl(node* root, const string& word, It c){ if(c == word.end()) return root->isWord; if(*c == '.'){ for(auto ptr : root->children){ if(ptr && searchImpl(ptr, word, c+1)) returntrue; } returnfalse; } if(root->children[*c - 'a']){ returnsearchImpl(root->children[*c - 'a'], word, c+1); } else returnfalse; } classnode{ public: array<node*, 26> children; bool isWord; node() :children{nullptr} ,isWord{false} {} ~node(){ for(auto ptr : children) delete ptr; } }; };