LeetCode 1032. Stream of Characters 4行Trie樹
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 lastk
characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.
Example:
StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary. 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
Note:
1 <= words.length <= 2000
1 <= 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.
----------------------------
有幾個值得學習的地方:
- 逆序,需要腦筋急轉彎
- reduce函式,reduce函式可以放三個引數(https://blog.csdn.net/taoqick/article/details/112157779)
- 當例項物件通過[] 運算子取值時,會呼叫它的方法__getitem__;也可以用在物件的迭代上。defaultdict.__getitem__(dic, 5)有兩個引數,分別是dict的例項和key。
- 別忘了剪枝
以下是程式碼:
from collections import defaultdict
class StreamChecker:
def __init__(self, words: List[str]):
add_child = lambda: defaultdict(add_child)
self.trie = defaultdict(add_child)
for word in words:
reduce(dict.__getitem__,word[::-1],self.trie)["$"] = True
self.history,self.max_len = "",max(map(len,words))
def query(self, letter: str) -> bool:
self.history = letter + self.history[:self.max_len-1]
node = self.trie
for ch in self.history:
if ch not in node:
return False
else:
node = node[ch]
if ("$" in node):
return True
return False
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)
相關文章
- Trie樹,字典樹
- 字典樹(Trie)
- hihocoder trie 樹
- 雙陣列TRIE樹Double-Array Trie理解引導陣列
- [leetcode/lintcode 題解] 微軟 面試題:實現 Trie(字首樹)LeetCode微軟面試題
- 線段樹也能是 Trie 樹 題解
- [LeetCode] 451. Sort Characters By FrequencyLeetCode
- LeetCode- Implement Trie (Prefix Tree)LeetCode
- 由簡入繁--Trie樹實戰
- Trie樹:字串頻率統計排序字串排序
- 【動畫】看動畫輕鬆理解「Trie樹」動畫
- 雙陣列字典樹(Double Array Trie)陣列
- hihocoder 1014 Trie樹 (Trie 記模板 陣列+指標)陣列指標
- 字串演算法--$\mathcal{KMP,Trie}$樹字串演算法KMP
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- Leetcode Longest Substring Without Repeating CharactersLeetCode
- [翻譯]資料結構——trie樹介紹資料結構
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- LeetCode之1 bit and 2 bit Characters(Kotlin)LeetCodeKotlin
- 【Leetcode】1081. Smallest Subsequence of Distinct CharactersLeetCode
- Leetcode-Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters leetcode javaLeetCodeJava
- hihocoder 1261 String Problem II (Trie樹)
- 208. 實現 Trie (字首樹)-pythonPython
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- LeetCode-Longest Substring with At Least K Repeating CharactersLeetCodeAST
- LeetCode-Longest Substring with At Most K Distinct CharactersLeetCode
- LeetCode OJ : 3 Longest Substring Without Repeating CharactersLeetCode
- 淺談樹形結構的特性和應用(上):多叉樹,紅黑樹,堆,Trie樹,B樹,B+樹...
- 雙陣列Trie樹高效構建有向無環圖陣列
- Java雙陣列Trie樹的實現方案總結Java陣列
- Trie
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- Leetcode-Longest Substring with At Most Two Distinct Characters.LeetCode
- Leetcode-Read N Characters Given Read4LeetCode
- LeetCode-Data Stream as Disjoint IntervalsLeetCode
- AC自動機+trie樹實現高效多模式匹配字典模式