classTrie { public: /** Initialize your data structure here. */ Trie() { root = new Node(); } /** Inserts a word into the trie. */ voidinsert(string word){ int start = -1; Node* point = root; while(++start < word.size()) { if(!travesalChilds(word, start, point)) { point->childs.push_back(new Node(word[start])); point = point->childs.back(); } } point->endOfWord = true; } /** Returns if the word is in the trie. */ boolsearch(string word){ Node* point = root; int start = -1; while(++start < word.size()) { if(!travesalChilds(word, start, point)) returnfalse; } return point->endOfWord; } /** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Node* point = root; int start = -1; while(++start < prefix.size()) { if(!travesalChilds(prefix, start, point)) returnfalse; } returntrue; } private: structNode { bool endOfWord = false; char val; vector<Node*> childs; Node() = default; Node(char val, bool end = false) : val(val), endOfWord(end) {}; }; Node* root; booltravesalChilds(string& s, int& start, Node*& node) { for(auto& child : node->childs) { if(child->val == s[start]) { node = child; returntrue; } } returnfalse; } };
/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */
classTrie { public: /** Initialize your data structure here. */ Trie() { root = new Node(); } /** Inserts a word into the trie. */ voidinsert(string word){ int start = -1; Node* point = root; while(++start < word.size()) { if(point->childs.find(word[start]) != point->childs.end()) point = point->childs[word[start]]; else point = point->childs.insert({word[start] ,new Node(word[start])}).first->second; } point->endOfWord = true; } /** Returns if the word is in the trie. */ boolsearch(string word){ Node* point = root; int start = -1; while(++start < word.size()) { if(point->childs.find(word[start]) != point->childs.end()) point = point->childs[word[start]]; else returnfalse; } return point->endOfWord; } /** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Node* point = root; int start = -1; while(++start < prefix.size()) { if(point->childs.find(prefix[start]) != point->childs.end()) point = point->childs[prefix[start]]; else returnfalse; } returntrue; } private: structNode { bool endOfWord = false; char val; unordered_map<char ,Node*> childs; Node() = default; Node(char val, bool end = false) : val(val), endOfWord(end) {}; }; Node* root; };
/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */
classTrie { public: /** Initialize your data structure here. */ Trie() { root = new Node(); } /** Inserts a word into the trie. */ voidinsert(string word){ int start = -1; Node* point = root; while(++start < word.size()) { if(point->childs[word[start] - 'a'] != nullptr) point = point->childs[word[start] - 'a']; else { point->childs[word[start] - 'a'] = new Node(word[start]); point = point->childs[word[start] - 'a']; } } point->endOfWord = true; } /** Returns if the word is in the trie. */ boolsearch(string word){ Node* point = root; int start = -1; while(++start < word.size()) { if(point->childs[word[start] - 'a'] != nullptr) point = point->childs[word[start] - 'a']; else returnfalse; } return point->endOfWord; } /** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Node* point = root; int start = -1; while(++start < prefix.size()) { if(point->childs[prefix[start] - 'a'] != nullptr) point = point->childs[prefix[start] - 'a']; else returnfalse; } returntrue; } private: structNode { bool endOfWord = false; char val; Node* childs[26]{ nullptr }; Node() = default; Node(char val, bool end = false) : val(val), endOfWord(end) {}; }; Node* root; };
/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */
} voidinsert(string word){ auto point = head; for(auto ch : word) { if(!point->childs[ch - 'a']) point->childs[ch - 'a'] = new Node(); point = point->childs[ch - 'a']; } point->isWord = true; } boolsearch(string word){ auto point = head; for(auto ch : word) { if(!point->childs[ch - 'a']) returnfalse; point = point->childs[ch - 'a']; } return point->isWord; } boolstartsWith(string prefix){ auto point = head; for(auto ch : prefix) { if(!point->childs[ch - 'a']) returnfalse; point = point->childs[ch - 'a']; } returntrue; } private: structNode { Node* childs[26]{}; bool isWord; Node() : isWord(false) {} }; private: Node *head; };
/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */