Python DFS | NO TLE ERROR 95%+ Time
The key to using DFS and not getting a TLE error is the max_length attribute.\n\nfrom collections import defaultdict\n\nclass TrieNode:\n    def __init__(self):
The key to using DFS and not getting a TLE error is the max_length attribute.\n```\nfrom collections import defaultdict\n\nclass TrieNode:\n    def __init__(self):\n        self.edges = {}\n        self.isWord = False\n\nclass WordDictionary:\n\n    def __init__(self):\n        self.head = TrieNode()\n        self.max_length = 0\n    def addWord(self, word: str) -> None:\n        curr = self.head\n        i = 0\n        for c in word:\n            #check if c is in edges, if yes, move to it,\n            if c not in curr.edges:\n                curr.edges[c] = TrieNode()\n            curr = curr.edges[c]\n            i += 1\n        #now mark isWord to True\n        curr.isWord = True\n        self.max_length = max(self.max_length, i)\n    def search(self, word: str) -> bool:\n\n        def dfs(j, head):\n            curr = head\n            \n            for i in range(j, len(word)):\n                c = word[i]\n                if c == ".":\n                    # if ., iterate over all edges and make a recusrive call\n                    for v in curr.edges.values():\n                        if dfs(i + 1, v):\n                            return True\n                    return False\n                else:\n                    if c not in curr.edges:\n                        return False\n                    curr = curr.edges[c]\n            return curr.isWord\n        \n        if len(word) > self.max_length:\n            return False\n\n        return dfs(0, self.head)\n\n```
['Depth-First Search', 'Recursion', 'Python', 'Python3']