def__init__(self): """ Initialize your data structure here. """ self.root = {"End":False}
definsert(self, word: str) -> None: """ Inserts a word into the trie. """ curNode = self.root for c in word: if c notin curNode.keys() : curNode[c] = {"End":False} curNode = curNode[c] curNode["End"] = True
defsearch(self, word: str) -> bool: """ Returns if the word is in the trie. """ curNode = self.root for c in word: if c notin curNode.keys() : returnFalse curNode = curNode[c] if curNode["End"] : returnTrue else: returnFalse
defstartsWith(self, prefix: str) -> bool: """ Returns if there is any word in the trie that starts with the given prefix. """ curNode = self.root for c in prefix: if c notin curNode.keys(): returnFalse else: curNode = curNode[c] returnTrue
def__init__(self): """ Initialize your data structure here. """ self.root = [0for i inrange(27) ]
definsert(self, word: str) -> None: """ Inserts a word into the trie. """ curNode = self.root for c in word: if curNode[ord(c)-ord("a")] ==0 : curNode[ord(c)-ord("a")] = [0for i inrange(27) ] curNode = curNode[ord(c)-ord("a")] curNode[26] = True
defsearch(self, word: str) -> bool: """ Returns if the word is in the trie. """ curNode = self.root for c in word: if curNode[ord(c)-ord("a")] == 0: returnFalse else: curNode = curNode[ord(c)-ord("a")] if curNode[26] : returnTrue else: returnFalse
defstartsWith(self, prefix: str) -> bool: """ Returns if there is any word in the trie that starts with the given prefix. """ curNode = self.root for c in prefix: if curNode[ord(c)-ord("a")] == 0: returnFalse else: curNode = curNode[ord(c)-ord("a")] returnTrue