You are viewing a single comment's thread. Return to all comments →
My code only passes 22/42 test case. can any one help to resolve this?
class TrieNode{ public: char data; TrieNode **children; bool isTerminal; TrieNode(char data){ this->data = data; children = new TrieNode*[10]; for(int i = 0; i < 10; i++){ children[i] = NULL; } isTerminal = false; } }; class Trie{ TrieNode *root; public: Trie(){ root = new TrieNode('\0'); } bool add(TrieNode *root, string word){ if(root->isTerminal){ return false; } if(word.size() == 0){ root->isTerminal = true; return true; } int index = word[0] - 'a'; TrieNode *child; if(root->children[index] != NULL){ child = root->children[index]; } else{ child = new TrieNode(word[0]); root->children[index] = child; } return add(child, word.substr(1)); } bool add(string word){ return add(root, word); } }; void noPrefix(vector<string> words) { Trie* trie = new Trie(); for(int i = 0; i < words.size(); i++){ if(!trie->add(words[i])){ cout<<"BAD SET"<<endl; cout<<words[i]<<endl; return; } } cout<<"GOOD SET"; }
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 →
My code only passes 22/42 test case. can any one help to resolve this?