def__init__(self): """ Initialize your data structure here. """ self.root = TrieNode()
definsert(self, word): """ Inserts a word into the trie. :type word: str :rtype: void """ node = self.root for w in word: if w notin node.chars: node.chars[w] = TrieNode() node = node.chars[w] node.is_word = True
defsearch(self, word): """ Returns if the word is in the trie. :type word: str :rtype: bool """ node = self.root for w in word: if w notin node.chars: returnFalse node = node.chars[w] return node.is_word
defstartsWith(self, prefix): """ Returns if there is any word in the trie that starts with the given prefix. :type prefix: str :rtype: bool """ node = self.root for w in prefix: if w notin node.chars: returnFalse node = node.chars[w] returnTrue
# 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)