Do you have TLE? ✅ Add this one thing to your code! 🔥
Hello!\n\nIf you get TLE, then consider storing length of the current longest word.\nIn search method you can return False if word is longer than current longes
Hello!\n\nIf you get TLE, then consider storing length of the current longest word.\nIn search method you can return False if word is longer than current longest word.\n\nExample of modified code from NeetCode:\n```\nclass TrieNode:\n    def __init__(self):\n        self.children = {}  # a : TrieNode\n        self.word = False\n\n\nclass WordDictionary:\n    def __init__(self):\n        self.root = TrieNode()\n        self.max_word_length = 0\n\n    def addWord(self, word: str) -> None:\n        cur = self.root\n        for c in word:\n            if c not in cur.children:\n                cur.children[c] = TrieNode()\n            cur = cur.children[c]\n        cur.word = True\n        \n        self.max_word_length = max(self.max_word_length, len(word))\n\n    def search(self, word: str) -> bool:\n        if len(word) > self.max_word_length:\n            return False\n        \n        def dfs(j, root):\n            cur = root\n\n            for i in range(j, len(word)):\n                c = word[i]\n                if c == ".":\n                    for child in cur.children.values():\n                        if dfs(i + 1, child):\n                            return True\n                    return False\n                else:\n                    if c not in cur.children:\n                        return False\n                    cur = cur.children[c]\n            return cur.word\n\n        return dfs(0, self.root)\n```\nPlease upvote if it helped you :))
['Python']