public bool rWordBreak(string s, int start, TrieNode root) { if (start == s.Length) return true; TrieNode cur = root; for (int i = start; i < s.Length; i++) { if (cur.Children.ContainsKey (s [i])) { if (cur.Children [s [i]].IsTerminal) { if (rWordBreak (s, i + 1, root)) { return true; } } cur = cur.Children [s [i]]; } else { return false; } } return false; }
public bool rSearch(string word, int start, TrieNode root) { if (start < word.Length - 1) { if (word [start] == '.') { bool find = false; foreach (var child in root.Children) { if (rSearch (word, start + 1, child.Value)) { return true; } } return false; } else { if (root.Children.ContainsKey (word [start])) { return rSearch (word, start + 1, root.Children [word [start]]); } else { return false; } } } else if (start == word.Length - 1) { if (word [start] == '.') { foreach (var child in root.Children) { if (child.Value.IsTerminal) { return true; } } return false; } else { return root.Children.ContainsKey (word [start]) && root.Children [word [start]].IsTerminal; } } return false; }
public Trie() { root = new TrieNode(); }