Design Add and Search Words Data Structure LeetCode solution in Python
1class TrieNode:23def __init__(self, val):4self.val = val5self.children = {}6self.isEnd = False789class WordDictionary:1011def __init__(self):12self.root = TrieNode("*")1314def addWord(self, word: str) -> None:1516curr = self.root1718for c in word:1920if c not in curr.children:21curr.children[c] = TrieNode(c)2223curr = curr.children[c]2425curr.isEnd = True2627def search(self, word: str) -> bool:2829def dfs(root, word):30curr = root3132for i in range(len(word)):3334if word[i] == ".":35for l in curr.children.values():36if dfs(l, word[i + 1 :]) == True:37return True3839return False4041if word[i] not in curr.children:42return False4344curr = curr.children[word[i]]4546return curr.isEnd4748return dfs(self.root, word)