Solving Leetcode Interviews in Seconds with AI: Stream of Characters
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1032" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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.
Explanation
Here's a solution to the problem, focusing on efficiency:
- Trie (Prefix Tree): We'll use a Trie data structure to efficiently store all the words in the input array. This allows us to quickly check if a suffix of the stream matches any of the words. To optimize suffix searching, we'll insert the words in reverse order into the Trie.
Stream Buffer: We maintain a buffer of the most recent characters from the stream. For each new character, we check if any suffix formed by the buffer matches a word in the Trie.
Runtime Complexity: O(m) per query, where m is the maximum length of the words in the dictionary. Preprocessing time is O(n*k) where n is the number of words, and k is the average length of the words. Storage Complexity: O(N), where N is the total number of characters across all words in the input.
Code
class TrieNode:
def __init__(self):
self.children = {}
self.is_word = False
class StreamChecker:
def __init__(self, words):
self.root = TrieNode()
self.stream = ""
self.max_len = 0
for word in words:
self.max_len = max(self.max_len, len(word))
word = word[::-1] # Reverse the word for suffix checking
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_word = True
def query(self, letter):
self.stream = letter + self.stream
self.stream = self.stream[:self.max_len] # Trim stream
node = self.root
for char in self.stream:
if char not in node.children:
return False
node = node.children[char]
if node.is_word:
return True
return False
# Example usage (same as provided in prompt):
# streamChecker = StreamChecker(["cd", "f", "kl"])
# print(streamChecker.query("a")) # False
# print(streamChecker.query("b")) # False
# print(streamChecker.query("c")) # False
# print(streamChecker.query("d")) # True
# print(streamChecker.query("e")) # False
# print(streamChecker.query("f")) # True
# print(streamChecker.query("g")) # False
# print(streamChecker.query("h")) # False
# print(streamChecker.query("i")) # False
# print(streamChecker.query("j")) # False
# print(streamChecker.query("k")) # False
# print(streamChecker.query("l")) # True