Words ending in a letter stream
Letters arrive one at a time. Write a class StreamWatcher, built from a list words, with a method push(letter): it receives the next letter and returns True if the letters received so far end with some word of words (the latest letters, read in order, spell that word), otherwise False.
Words use lowercase a–z and have 1 to 20 letters; words may be empty or contain repeats, and the stream uses lowercase a–z too. Each push should cost O(length of the longest word), not a check of every word.
The tests call the provided run_watcher(words, stream): it builds your class and returns the result of push for each letter of stream, in order.
Besides the tests, one large input (2,000 words, 7,000 letters) with a ~250 ms limit (scaled to your device's speed): brute force won't pass.
Examples
run_watcher(["at","q","rst"], "matqrst")→[false,false,true,true,false,false,true]run_watcher(["he","she","hers"], "ushers")→[false,false,false,true,false,true]run_watcher([], "abc")→[false,false,false]