definsert(self, word: str) -> None: it = self.head for i in range(len(word)): # if word[i] not in it.children: #it.children[word[i]] = TrieNode() it = it.children[word[i]] it.is_end = True
defsearch(self, word: str) -> bool: it = self.head for i in range(len(word)): if word[i] notin it.children: returnFalse it = it.children[word[i]] return it.is_end
defstartsWith(self, prefix: str) -> bool: it = self.head for i in range(len(prefix)): if prefix[i] notin it.children: returnFalse it = it.children[prefix[i]] returnTrue
definsert(self, word: str) -> None: ifnot word: return it = self.head for i in range(len(word)): # if word[i] not in it.children: # it.children[word[i]] = TrieNode() it = it.children[word[i]] if i == len(word) - 1: it.is_end = True
defsearch(self, word: str) -> bool: ifnot word: returnFalse it = self.head for i in range(len(word)): if word[i] notin it.children: returnFalse it = it.children[word[i]] if i == len(word) - 1and it.is_end: returnTrue returnFalse
defstartsWith(self, prefix: str) -> bool: ifnot prefix: returnFalse it = self.head for i in range(len(prefix)): if prefix[i] notin it.children: returnFalse it = it.children[prefix[i]] if i == len(prefix) - 1: returnTrue returnFalse