R4-位运算
Trie树+BFS处理'.'
class WordDictionary:
def __init__(self):
self.root={}
def addWord(self, word: str) -> None:
node=self.root
for c in word:
if c not in node:
node[c]={}
node=node[c]
node["#"]={}
def search(self, word: str) -> bool:
word+="#"
queue=deque([self.root])
for c in word:
length=len(queue)
for _ in range(length):
node=queue.popleft()
if c==".":
for v in node.values():
queue.append(v)
elif c in node:
queue.append(node[c])
if not queue:
return False
return True
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
有点难懂说实话
ps: