private void BuildTrier(string[] words) { Root = new TrierNode(' '); foreach (var word in words) { var current = Root; foreach (var wc in word) { if (!current.Children.ContainsKey(wc)) { current.Children[wc] = new TrierNode(wc); } current = current.Children[wc]; } current.IsEndingChar = true; } }
public bool Query(char letter) { TrierNode longest = null; while (current != null) { if (current.Children.TryGetValue(letter, out TrierNode child)) { if (longest == null) { longest = child; } if (child.IsEndingChar) { current = longest; return(true); } } current = current.Fail; } current = longest ?? trier.Root; return(false); }
public StreamChecker(string[] words) { // build trier and fail pointer trier = new Trier(words); current = trier.Root; }