We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
C++11 variant with Trie. As it was pointed out tests want you to implement exactly this solution.
classTrieNode{public:unordered_map<char,shared_ptr<TrieNode>>children;boolisEndOfWord;TrieNode():isEndOfWord(false){}};classTrie{public:Trie():root(newTrieNode()){}boolinsert(string&word){autonode=root;for(autoch:word){if(!node->children.count(ch)){node->children[ch]=shared_ptr<TrieNode>(newTrieNode());}node=node->children[ch];// Exit condition #1: This node is an end of some wordif(node->isEndOfWord){returnfalse;}}node->isEndOfWord=true;// Exit condition #2: This node is a prefix for some wordif(!node->children.empty()){returnfalse;}returntrue;}private:shared_ptr<TrieNode>root;};voidnoPrefix(vector<string>words){Trietrie;for(auto&word:words){if(!trie.insert(word)){cout<<"BAD SET"<<endl;cout<<word<<endl;return;}}cout<<"GOOD SET"<<endl;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
C++11 variant with Trie. As it was pointed out tests want you to implement exactly this solution.