definsert(self, word): """ Inserts a word into the trie. :type word: str :rtype: None """ node = self.root for c in word: if c notin node.children: node.children[c] = TrieNode() node = node.children[c] node.is_word = True
defsearch_prefix(self, word): node = self.root for c in word: if c notin node.children: returnNone node = node.children[c] return node
defsearch(self, word): """ Returns if the word is in the trie. :type word: str :rtype: bool """ node = self.search_prefix(word) return node isnotNoneand 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 """ returnself.search_prefix(prefix) isnotNone