#1032

Stream of Characters

candidate master · 1380 · lc hard +32 · 52% accepted · 1,878 likes · top 42%

Description

Design a data structure that reads a stream of characters and, after each new character, checks whether any suffix of the stream so far matches a word in the given words array.

Implement the StreamChecker class:

- StreamChecker(String[] words) Initializes the object with the strings array words.

- boolean query(char letter) Accepts a new character and returns true if any non-empty suffix of the stream 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]

Example 2:

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

Code

1
2
3
4
5
6
7
8
9
10
11
12