LeetCode 1032: Stream of Characters Solution
Master LeetCode problem 1032 (Stream of Characters), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1032. Stream of Characters
Problem Explanation
Explanation:
To solve this problem efficiently, we can use a trie data structure. We will reverse each word in the input array and store these reversed words in a trie. This allows us to efficiently check if any suffix of the characters in the stream forms a word in the input array.
We will also maintain a list of pointers that point to the current position in the trie for each character in the stream. Whenever we receive a new character, we update these pointers to the next nodes in the trie and check if any of the pointers reach a word node.
-
Time Complexity:
- Constructing the trie and querying each character takes O(N) time, where N is the total number of characters in all words.
- Each query operation takes O(L) time, where L is the average length of words in the input array.
- Overall, the time complexity for each query operation is O(L).
-
Space Complexity:
- The space complexity for storing the trie is O(N), where N is the total number of characters in all words.
- Additional space is used to store pointers for each character in the stream.
:
Solution Code
class TrieNode {
TrieNode[] children;
boolean isWord;
public TrieNode() {
children = new TrieNode[26];
isWord = false;
}
}
class StreamChecker {
TrieNode root;
List<TrieNode> stream;
public StreamChecker(String[] words) {
root = new TrieNode();
stream = new ArrayList<>();
for (String word : words) {
TrieNode curr = root;
for (int i = word.length() - 1; i >= 0; i--) {
char ch = word.charAt(i);
if (curr.children[ch - 'a'] == null) {
curr.children[ch - 'a'] = new TrieNode();
}
curr = curr.children[ch - 'a'];
}
curr.isWord = true;
}
}
public boolean query(char letter) {
boolean found = false;
stream.add(root);
List<TrieNode> newStream = new ArrayList<>();
for (TrieNode node : stream) {
if (node.children[letter - 'a'] != null) {
newStream.add(node.children[letter - 'a']);
if (node.children[letter - 'a'].isWord) {
found = true;
}
}
}
stream = newStream;
return found;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1032 (Stream of Characters)?
This page provides optimized solutions for LeetCode problem 1032 (Stream of Characters) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1032 (Stream of Characters)?
The time complexity for LeetCode 1032 (Stream of Characters) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1032 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1032 in Java, C++, or Python.