Stream of Characters

Design a data stream checker that returns whether any suffix of the current stream matches one of the inserted words. Pattern focus: Trie Node Structure. Reversing the dictionary words turns suffix checks into prefix traversal on the trie.

Input Format

words = dictionary words, queries = streamed characters in order

Output Format

boolean results after each streamed character

Constraints

  • 1 <= words.length <= 2 * 10^4
  • 1 <= queries.length <= 10^5
  • Words contain only lowercase English letters.

Examples

Example 1:

Input:

words = ["cd","f","kl"]
queries = ["a","b","c","d","e","f","g","h","i","j","k","l"]

Output:

[false,false,false,true,false,true,false,false,false,false,false,true]

Explanation:

A suffix match appears when the stream ends with cd, f, or kl.

Example 2:

Input:

words = ["a"]
queries = ["a"]

Output:

[true]

Explanation:

A one-letter dictionary word matches immediately.

Example 3:

Input:

words = ["ab","ba","aa"]
queries = ["a","b","a","a"]

Output:

[false,true,true,true]

Explanation:

Different suffixes can become valid as the stream grows.

Loading...
Stream of Characters - Trie DSA Problem