1032. Stream of Characters
Description
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 arraywords.boolean query(char letter)Accepts a new character from the stream and returnstrueif any non-empty suffix from the stream forms a word that is inwords.
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 <= 20001 <= words[i].length <= 200words[i]consists of lowercase English letters.letteris a lowercase English letter.- At most
4 * 104calls will be made to query.
Solutions
Solution: Trie
- Time complexity:
- constructor: O(words.length*words[i].length)
- query: O(query call times)
- Space complexity:
- constructor: O(words.length*words[i].length)
- query: O(query call times)
JavaScript
js
/**
* @param {string[]} words
*/
const StreamChecker = function (words) {
const trie = {};
for (const word of words) {
const n = word.length;
let current = trie;
for (let index = n - 1; index >= 0; index--) {
const letter = word[index];
if (!current[letter]) {
current[letter] = {};
}
current = current[letter];
}
current.isWord = true;
}
this.trie = trie;
this.stream = [];
};
/**
* @param {character} letter
* @return {boolean}
*/
StreamChecker.prototype.query = function (letter) {
this.stream.push(letter);
const n = this.stream.length;
let current = this.trie;
for (let index = n - 1; index >= 0; index--) {
const char = this.stream[index];
if (!current[char]) return false;
current = current[char];
if (current.isWord) return true;
}
return false;
};
/**
* Your StreamChecker object will be instantiated and called as such:
* var obj = new StreamChecker(words)
* var param_1 = obj.query(letter)
*/