private bool WordBreak(string str, int start, Trie trie, bool[] memo) { // Base case if (start == str.Length) { return(true); } if (memo[start]) { return(false); } Trie.Node cur = trie.Root; // Try all prefixes of lengths from 1 to size for (int i = start; i < str.Length; i++) { cur = cur.Get(str[i]); if (cur == null) { memo[start] = true; return(false); } if (cur.IsTerminal == true && WordBreak(str, i + 1, trie, memo)) { return(true); } } memo[start] = true; // If we have tried all prefixes and none // of them worked return(false); }
public TrieSearchResult(Trie.Node terminalNode, bool containsPrefix, bool containsWord) { TerminalNode = terminalNode; ContainsPrefix = containsPrefix; ContainsWord = containsWord; }