Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mvallebr/186486b9e0c05fa80ca0f72d14446b4d to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/186486b9e0c05fa80ca0f72d14446b4d to your computer and use it in GitHub Desktop.
class WordDictionary:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = {}
def addWord(self, word: str) -> None:
last = self.root
for i in range(len(word)):
if word[i] not in last:
last[word[i]] = {}
last = last[word[i]]
last["*"] = {}
def search(self, word: str) -> bool:
def search_i(node: dict, index: int) -> bool:
if index == len(word):
return "*" in node
c = word[index]
if c != '.':
if c not in node:
return False
else:
return search_i(node[c], index + 1)
else:
for c, child in node.items():
if search_i(child, index + 1):
return True
return False
return search_i(self.root, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment