public static bool WordExists(MyTrie head, string word) { MyTrie node = head; for (int i = 0; i < word.Length; i++) { if (node.Nodes[word[i] - 'a'] == null) { return(false); } node = node.Nodes[word[i] - 'a']; } return(node.IsWord); }
public bool CanBreakBottomUp(string[] dict, string str) { MyTrie head = new MyTrie(); foreach (string word in dict) { MyTrie.AddWord(head, word); } bool[] good = new bool[str.Length + 1]; good[0] = true; for (int i = 0; i < str.Length; i++) { if (good[i]) { MyTrie node = head; for (int j = i; j < str.Length; j++) { if (node == null) { break; } node = node.Nodes[str[j] - 'a']; if (node != null && node.IsWord) { good[j + 1] = true; } } } } return(good[str.Length]); }