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.
classTrieNode{publicTrieNode[]Child=newTrieNode[10];publicboolWordEnd=false;}classTrie{publicTrieNodeRoot=newTrieNode();// Inserts s into the Trie and also checks if s is a prefix or has a prefix in the Trie.publicboolInsert(strings){boolhasPrefix=false;boolisPrefix=true;TrieNodecurrentNode=this.Root;foreach(charchins){if(currentNode.WordEnd){hasPrefix=true;}intindex=ch-'a';if(currentNode.Child[index]==null){currentNode.Child[index]=new();isPrefix=false;}currentNode=currentNode.Child[index];}currentNode.WordEnd=true;returnhasPrefix||isPrefix;}}classResult{publicstaticvoidnoPrefix(List<string>words){Trietrie=new();foreach(stringwordinwords){boolprefix=trie.Insert(word);if(prefix){Console.WriteLine("BAD SET");Console.WriteLine(word);return;}}Console.WriteLine("GOOD SET");}}
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# solution using a Trie