Skip to content
BytePatterns

Word Endings in a Letter Stream

HardTries#trie#reversed-trie#stream~40m

Problem

A filter watches a chat message as it is typed, one letter at a time. It is given a list of banned words up front, and after each new letter it must report whether the text typed so far ends with any banned word. The stream can be very long, so each letter should cost time bounded by the length of the longest banned word, not by the length of the stream.

Examples

Input:  words = ["cd", "f", "kl"], stream = "abcdefghijkl"
Output: [F, F, F, T, F, T, F, F, F, F, F, T]
Why:    the text ends with "cd" after d, with "f" after f and with "kl" after l
Input:  words = ["ab", "bab"], stream = "bab"
Output: [F, F, T]
Why:    after the last letter the text ends with both "ab" and "bab"
Input:  words = ["xyz"], stream = "xy"
Output: [F, F]
Why:    edge case, the word is never completed

Hints

0 / 3

Stuck on the idea rather than the code? Word Search With a Trie covers it.