Stream of Characters
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Trie.
Stream of Characters
Design a data structure that accepts a stream of characters and checks if any suffix of the characters queried so far is in a given list of words.
Examples
Input: words = ["cd","f","kl"], streamQueries = ["a", "b", "c", "d", "e", "f"]
Output: [false, false, false, true, false, true]
Approach 1
Level I: Brute Force Suffix Comparison
Intuition
Store all arrived characters in a list. For every query, iterate through all words in the dictionary and check if any word matches the current suffix of the character stream. O(N * L) per query.
⏱ O(N * L) per query.💾 O(Total Chars)
Approach 2
Level III: Reverse Trie
Intuition
Since we need to match a suffix of the stream, it's efficient to store the dictionary words in a Trie in reversed order. As new characters arrive in the stream, we store them in a buffer (or current path). To check if any suffix matches, we traverse the reversed Trie starting from the last character received.
⏱ O(L) per query (L is max word length).💾 O(N * L).
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.