Created
February 23, 2021 15:55
-
-
Save mvallebr/186486b9e0c05fa80ca0f72d14446b4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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