def__init__(self): """ Initialize your data structure here. """ self.root = {} self.end_of_word = '#'
definsert(self, word: str) -> None: """ Inserts a word into the trie. """ node = self.root for char in word: node = node.setdefault(char, {}) node[self.end_of_word] = self.end_of_word
defsearch(self, word: str) -> bool: """ Returns if the word is in the trie. """ node = self.root for char in word: if char notin node: returnFalse node = node[char] return self.end_of_word in node
defstartsWith(self, prefix: str) -> bool: """ Returns if there is any word in the trie that starts with the given prefix. """ node = self.root for char in prefix: if char notin node: returnFalse node = node[char] returnTrue