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{publicDictionary<char,TrieNode>children{get;set;}publicboolIsLeaf{get;set;}=false;publicstringWord{get;set;}=string.Empty;publicTrieNode(){children=newDictionary<char,TrieNode>();}}classTrie{publicTrieNoderoot{get;set;}publicTrie(){root=newTrieNode();}publicboolAddWord(stringword){TrieNodecurrent=root;foreach(charcinword.ToCharArray()){if(!current.children.ContainsKey(c)){current.children.Add(c,newTrieNode());current=current.children[c];}else{current=current.children[c];//if current node already exists, and is marked as a leaf// this means that a prefix for the current word already existsif(current.IsLeaf){Console.WriteLine("BAD SET");Console.WriteLine(word);returnfalse;}}}// if current node has children, this means that the current word is a prefix for a word that already existsif(current.children.Count>0){Console.WriteLine("BAD SET");Console.WriteLine(word);returnfalse;}current.IsLeaf=true;current.Word=word;returntrue;}}classResult{/* * Complete the 'noPrefix' function below. * * The function accepts STRING_ARRAY words as parameter. */publicstaticvoidnoPrefix(List<string>words){Trietrie=newTrie();foreach(stringwordinwords){if(!trie.AddWord(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 Trie