Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1032. Stream of Characters

Hard52.3% Acceptance
ArrayStringDesign
Asked by:
G
Google
ProblemHints (1)Solutions (4)VideosCompanies (4)Notes

Problem Statement

Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings words.

For example, if words = ["abc", "xyz"] and the stream added the four characters (one by one) 'a', 'x', 'y', and 'z', your algorithm should detect that the suffix "xyz" of the characters "axyz" matches "xyz" from words.

Implement the StreamChecker class:

  • StreamChecker(String[] words) Initializes the object with the strings array words.
  • boolean query(char letter) Accepts a new character from the stream and returns true if any non-empty suffix from the stream forms a word that is in words.

Example 1:

Input
["StreamChecker", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query"]
[[["cd", "f", "kl"]], ["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"]]
Output
[null, false, false, false, true, false, true, false, false, false, false, false, true]

Explanation
StreamChecker streamChecker = new StreamChecker(["cd", "f", "kl"]);
streamChecker.query("a"); // return False
streamChecker.query("b"); // return False
streamChecker.query("c"); // return False
streamChecker.query("d"); // return True, because 'cd' is in the wordlist
streamChecker.query("e"); // return False
streamChecker.query("f"); // return True, because 'f' is in the wordlist
streamChecker.query("g"); // return False
streamChecker.query("h"); // return False
streamChecker.query("i"); // return False
streamChecker.query("j"); // return False
streamChecker.query("k"); // return False
streamChecker.query("l"); // return True, because 'kl' is in the wordlist

Constraints:

  • 1 <= words.length <= 2000
  • 1 <= words[i].length <= 200
  • words[i] consists of lowercase English letters.
  • letter is a lowercase English letter.
  • At most 4 * 104 calls will be made to query.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
S
Salesforce
A
Amazon
M
Microsoft

Approach

The key challenge in #1032 Stream of Characters is efficiently checking whether the current stream of queried characters forms a suffix that matches any word from a given dictionary. Since queries arrive one character at a time, repeatedly scanning all words would be too slow.

A common strategy is to use a Trie (prefix tree), but with a twist: store each word in reversed order. As characters arrive from the stream, maintain a running buffer of recent characters and traverse the Trie from the newest character backward. This effectively converts the suffix-matching problem into a prefix search inside the Trie.

If during traversal you reach a node marked as the end of a word, a match exists. This approach significantly reduces repeated comparisons and allows quick incremental checks for each query.

With a well-structured Trie and bounded stream length, each query can be processed efficiently while keeping memory usage manageable.

Complexity

ApproachTime ComplexitySpace Complexity
Reversed Trie with Stream BufferO(L) per query, where L is the maximum word lengthO(N * L) for storing words in the Trie

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Put the words into a trie, and manage a set of pointers within that trie.

Ready to see the solutions?View Solutions

Solutions (4)

Approach 1: Trie with Reverse Words

This approach utilizes a reversed Trie (prefix tree) to efficiently manage and query suffixes of input characters. By storing the reversed words in a Trie, we can match suffixes of the input stream by querying from the current character backwards. For each character queried, we move through the Trie from those characters up to a certain limit (or until no further match) to see if any of the suffixes match one of the stored words.

Time Complexity for insertion: O(N*L), where N is the number of words and L is the maximum length of a word.
Time Complexity for queries: O(Q*L) where Q is the number of queries and L is the maximum length of a word.
Space Complexity: O(N*L) for storing the words in the Trie.

PythonJava
1class TrieNode:
2    def __init__(self):
3        self.children = {}
4        self.is_end_of_word = False
5
6class StreamChecker:
7    def __init__(self, words):
8        self.root = TrieNode()
9        self.stream = []
10        for word in words:
11            node = self.root
12            for char in reversed(word):
13                if char not in node.children:
14                    node.children[char] = TrieNode()
15                node = node.children[char]
16            node.is_end_of_word = True
17
18    def query(self, letter):
19        self.stream.append(letter)
20        node = self.root
21        for char in reversed(self.stream):
22            if node.is_end_of_word:
23                return True
24            if char not in node.children:
25                return False
26            node = node.children[char]
27        return node.is_end_of_word

Explanation

The implementation involves creating a Trie that stores each word in reverse. When a query is made, characters are added to a list representing the current stream. We then check from the current end of the stream backwards through the Trie to determine if any suffix matches a word.

Approach 2: Aho-Corasick Automaton

The Aho-Corasick algorithm is useful for searching multiple 'dictionary' words within a text. We build an automaton with all words and query the stream letters. This data structure enables matching of all dictionary words in parallel through a finite state machine that jumps quickly between states even upon mismatches.

Time Complexity for building: O(N*L), because we insert and construct failure links.
Time Complexity for queries: O(Q*L) leveraging finite state machine transitions.
Space Complexity: O(N*L) due to the state-space storage representing words.

C++JavaScript
1#include <vector>
2#include <queue>
3#include <unordered_map>
4using namespace std;
5
class AhoCorasick {
public:
    struct TrieNode {
        unordered_map<char, TrieNode*> children;
        TrieNode* fail = nullptr;
        bool is_end = false;
    };

    TrieNode *root = new TrieNode();

    void insert(const string &word) {
        TrieNode *node = root;
        for(char c : word) {
            if(!node->children.count(c)) {
                node->children[c] = new TrieNode();
            }
            node = node->children[c];
        }
        node->is_end = true;
    }

    void build() {
        queue<TrieNode*> q;
        root->fail = root;
        for(auto &p : root->children) {
            p.second->fail = root;
            q.push(p.second);
        }
        while(!q.empty()) {
            TrieNode *curr = q.front(); q.pop();
            for(auto &p : curr->children) {
                char c = p.first;
                TrieNode *child = p.second;
                TrieNode *fail = curr->fail;
                while(fail != root && !fail->children.count(c)) {
                    fail = fail->fail;
                }
                if(fail->children.count(c)) {
                    child->fail = fail->children[c];
                } else {
                    child->fail = root;
                }
                child->is_end |= child->fail->is_end;
                q.push(child);
            }
        }
    }
};

class StreamChecker {
    AhoCorasick ac;
    AhoCorasick::TrieNode* node;
    public:
    StreamChecker(vector<string>& words) {
        for(const string &word : words) {
            ac.insert(word);
        }
        ac.build();
        node = ac.root;
    }

    bool query(char letter) {
        while(node != ac.root && !node->children.count(letter)) {
            node = node->fail;
        }
        if(node->children.count(letter)) {
            node = node->children[letter];
        }
        return node->is_end;
    }
};

Video Solutions

Watch expert explanations and walkthroughs

Longest Substring Without Repeating Characters - Leetcode 3 - Python

NeetCode
6:46657,697 views

Asked By Companies

4 companies
G
Google
S
Salesforce
A
Amazon
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Group AnagramsMedium
Text JustificationHard
Word SearchMedium
Word BreakMedium
More similar problems

Related Topics

ArrayStringDesignTrieData Stream

Problem Stats

Acceptance Rate52.3%
DifficultyHard
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Stream of Characters asked in FAANG interviews?

Yes, problems like Stream of Characters are commonly discussed in FAANG-style interviews because they test knowledge of Tries, streaming data handling, and efficient query design. It also evaluates a candidate's ability to optimize repeated lookups.

What is the optimal approach for Stream of Characters?

The optimal approach uses a Trie where words are inserted in reversed order. As characters stream in, you traverse the Trie using the recent characters in reverse to check if any suffix matches a word. This allows efficient query processing without rechecking every dictionary word.

Why are words reversed in the Trie for Stream of Characters?

Words are reversed so that suffix matching becomes equivalent to prefix traversal in the Trie. When a new character arrives, you traverse from the latest character backward and follow Trie edges until a match or failure occurs.

What data structure is best for solving Stream of Characters?

A Trie (prefix tree) is the most suitable data structure for this problem. By reversing the words before inserting them, the Trie enables fast suffix matching as characters arrive in the stream.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

Explanation

This C++ solution implements the Aho-Corasick algorithm:

  • First, insert all reversed words from the list into the automaton.
  • Second, create the 'failure function' links between Trie nodes for efficient backtracking.
  • During each query, the automaton transitions through states, allowing for efficient suffix matching.