Trigger When A Streamed Suffix Matches A Keyword
A streaming moderation layer reads characters of a model's output one at a time. It must fire the instant the most recent characters spell any banned keyword. Design a matcher that, after each character, reports whether some watch-word is a suffix of the stream so far.
Problem
Implement StreamChecker(words) and a method query(letter) that returns true if any word in the initial list is a suffix of the characters queried so far (in order). Characters arrive one at a time.
Input
A list words for construction; a sequence of single-character query() calls.
Output
A boolean for each query indicating a suffix match.
Constraints
- 1 ≤ words.length ≤ 2000
- 1 ≤ word length ≤ 200
- Up to 4e4 query() calls.
- Lowercase English letters.
Examples
Example 1
Input
words=["cd","f","kl"]; query c,a,b,c,d → ...,true; then f → true
Output
false,false,false,false,true,true
After 'cd' arrives the suffix matches 'cd'; later 'f' matches 'f'.
Example 2
Input
words=["abc"]; query a,b,c
Output
false,false,true
The suffix 'abc' matches after the third character.