private bool SearchDfs(EnglishTrieNode node, string word, int index) { var curr = node; for (var i = index; i < word.Length; i++) { //是点要递归 if (word[i] == '.') { //不能再这里提前返回false,在递归调用前返回的话,因为我们认为深层递归失败只是一条路失败,不会返回false,会造成超长字符串永远匹配成功的情况 var flag = 0; foreach (var child in curr.Children) { if (child != null) { //能找到一条路径才是true,但是没找到不能算false,因为其他路径可能还没尝试过 if (SearchDfs(child, word, i + 1)) { return(true); } else { flag++; } } else { flag++; } } if (flag == 26) { return(false); } } else { if (!curr.TryGet(word[i], out curr)) { return(false); } } } return(curr.IsEnd); }
public WordDictionary() { Root = new EnglishTrieNode(); }
public ImplementTrie() { Root = new EnglishTrieNode(); }