definsert(self, word: str) -> None: it = self.head for i inrange(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 inrange(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 inrange(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 inrange(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 inrange(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 inrange(len(prefix)): if prefix[i] notin it.children: returnFalse it = it.children[prefix[i]] if i == len(prefix) - 1: returnTrue returnFalse
defcalculate(self, s: str) -> int: # prev_num[prev_op]num, 2+3+4+, 2+3*4+ stack, prev_op, num = [], '+', 0 s += "+" for c in s: if c == "": continue if c.isdigit(): num = num * 10 + int(c) elif c in'+-*/': if prev_op in'*/': prev_num = stack.pop() if prev_op == "*": num = prev_num * num else: num = int(prev_num / num) if prev_op == "-": num = -num stack.append(num) num = 0 prev_op = c returnsum(stack)