Python 3    ||   T/S: 99% / 99%
[UPDATED 5/12/2023]\n\nSince the initial post, this problem\'s constraints and test cases were amended such that, as two WLB comments below correctly point out,
[UPDATED 5/12/2023]\n\nSince the initial post, this problem\'s constraints and test cases were amended such that, as two WLB comments below correctly point out, the code I posted initially TLEs now.\n\nI had another version (now posted below) which was initially slower than the initial post, but now is the faster code. (Well, at least it doesn\'t TLE...)\n\nIs it a better solution? It is now, based on the new constraints and test cases. Was it a better solution before those changes? I don\'t know for sure, but xyztNull\'s excellent comment below addresses that matter.\n```\n```\nInitial post, which now TLEs:\n```\nclass WordDictionary:\n        def __init__(self):\n\n            self.words = defaultdict(list)\n\n\n        def addWord(self, word: str) -> None:\n\n            self.words[len(word)].append(word)\n\n\n        def search(self, word: str) -> bool:\n\n            n = len(word)\n\n            if \'.\' in word:\n                \n                for w in self.words[n]:\n                    if all(word[i] in (w[i], \'.\') for i in range(n)):\n                        return True\n\n                else: return False\n\n            return word in self.words[n]\n```\n[https://leetcode.com/problems/design-add-and-search-words-data-structure/submissions/629354737/](https://leetcode.com/problems/design-add-and-search-words-data-structure/submissions/629354737/)\n\n\n\nThe initially slower code, now faster:\n```\nclass Node:\n    \n    def __init__(self):\n        \n        self.d = defaultdict(Node)\n        self.EOW = False\n        \n\nclass WordDictionary:\n    \n    def __init__(self):\n        self.words = Node()\n        \n\n    def addWord(self, word: str) -> None:\n        \n        cur = self.words\n        for ch in word: cur = cur.d[ch]\n        cur.EOW = True\n\n\n    def search(self, word: str) -> bool:\n        \n        return self.dfs(word, self.words)   \n\n    \n    def dfs(self, word: str, node: Node, i = 0) -> bool:\n        \n        if not node         : return False\n        if i == len(word)   : return node.EOW\n\n        if word[i] == \'.\'   : return any(\n                                (self.dfs(word, child, i+1)\n                                for child in node.d.values()))\n\n        return self.dfs(word, node.d.get(word[i]), i+1)\n```\n[https://leetcode.com/problems/design-add-and-search-words-data-structure/submissions/949187192/](https://leetcode.com/problems/design-add-and-search-words-data-structure/submissions/949187192/)
['Python3']