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.
Test case 1 answer is 'd' is actually correct one, because you insert 'd' after you insert 'dbajkhbhbjdh' (the previous string), and right at that time, the function should determine that the word array is a BAD SET, so it should print out BAD SET d. Below are my JS code which apply Trie structure.
classTrieNode{constructor(){this.children={};this.isEndOfString=false;this.count=0;}}classTrie{constructor(){this.root=newTrieNode();}insert(word){letnode=this.root;for(letcharofword){if(!node.children[char]){node.children[char]=newTrieNode()}node=node.children[char];node.count+=1;}node.isEndOfString=true;}isGoodSet(word){letnode=this.root;for(leti=0;i<word.length;i++){node=node.children[word[i]];if(node.isEndOfString===true&&node.count>1)returnfalse;}returntrue;}}functionnoPrefix(words){// Write your code herelettrie=newTrie();for(letwordofwords){trie.insert(word);if(!trie.isGoodSet(word)){console.log('BADSET');console.log(word);return;}}console.log('GOODSET');}
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 →
Test case 1 answer is 'd' is actually correct one, because you insert 'd' after you insert 'dbajkhbhbjdh' (the previous string), and right at that time, the function should determine that the word array is a BAD SET, so it should print out BAD SET d. Below are my JS code which apply Trie structure.