# 692import collections
classNode:def__init__(self):
self.children = collections.defaultdict(Node)
self.isword =FalseclassTrie:def__init__(self):"""
Initialize your data structure here.
"""
self.root = Node()definsert(self, word:str)->None:"""
Inserts a word into the trie.
"""
cur = self.root
for w in word:
cur = cur.children[w]
cur.isword =Truedefsearch(self, word:str)->bool:"""
Returns if the word is in the trie.
"""
cur = self.root
for w in word:
cur = cur.children.get(w)if cur isNone:returnFalsereturn cur.isword
defstartsWith(self, prefix:str)->bool:"""
Returns if there is any word in the trie that starts with the given prefix.
"""
cur = self.root
for w in prefix:
cur = cur.children.get(w)if cur isNone:returnFalsereturnTrue# Your Trie object will be instantiated and called as such:# obj = Trie()# obj.insert(word)# param_2 = obj.search(word)# param_3 = obj.startsWith(prefix)