Design Add and Search Words Data Structure

Solution Code
def search(self, word: str) -> bool:
        def dfs(index, node):
            if index == len(word):
                return node.is_word
            
            char = word[index]
            if char == '.':
                for child in node.children.values():
                    if dfs(index + 1, child):
                        return True
                return False
Video GuideLeetcode Daily

Time Complexity

O(M) for insertion, up to O(\Sigma^M) for search

Space Complexity

O(M × N) to store the nodes