You are viewing a single comment's thread. Return to all comments →
Hewre is a C# solution that uses a simple implementation of a trie using Dictionaries. Passes all test cases:
public static void noPrefix(List<string> words) { var isGood = true; var root = new Dictionary<char, object> { }; var catchWord = string.Empty; foreach (var word in words) { if (!InsertWord(root, word)) { isGood = false; catchWord = word; break; } } var result = isGood ? "GOOD SET" : "BAD SET"; Console.WriteLine(result); if (!isGood) Console.WriteLine(catchWord); } public static bool InsertWord(Dictionary<char, object> theTrie, string w) { const char X = 'X'; var firstC = w[0]; var success = !theTrie.TryGetValue(firstC, out var nextTrie) || !((Dictionary<char, object>)nextTrie).TryGetValue(X, out _); if (!success) return false; if (nextTrie == null) { nextTrie = new Dictionary<char, object> { }; theTrie[firstC] = nextTrie; } if (w.Length == 1) { if (((Dictionary<char, object>)nextTrie).Any()) return false; ((Dictionary<char, object>)nextTrie)[X] = true; return true; } return InsertWord((Dictionary<char, object>)nextTrie, w.Substring(1)); }
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 →
Hewre is a C# solution that uses a simple implementation of a trie using Dictionaries. Passes all test cases: