题目
Implement the StreamChecker class as follows:
StreamChecker(words): Constructor, init the data structure with the given words.query(letter): returns true if and only if for somek >= 1, the lastkcharacters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.
Example:
1 | StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary. |
Note:
1 <= words.length <= 20001 <= words[i].length <= 2000- Words will only consist of lowercase English letters.
- Queries will only consist of lowercase English letters.
- The number of queries is at most 40000.
思路
Python by search in Trie [w/ Visualization]
代码
1 | from collections import defaultdict |