Python Trie Solution 6170 ms
My solution is very similar to other people\'s answers so I will just say how mine is different.\n\nI will remember the length of the longest given word, so if 
My solution is very similar to other people\'s answers so I will just say how mine is different.\n\nI will remember the length of the longest given word, so if someone tries to \'look up\' a word that consists of more letters than the longest of our available words, then we can automatically return False.\n\nThis way our algorithm will be a lot faster.\nPlease ask any questions if something is not clear to you, after all, my code is pretty messy.\n\n\tclass TrieNode:\n\t\tdef __init__(self):\n\t\t\tself.children = dict()\n\t\t\tself.is_word = False\n\n\n\tclass Trie:\n\t\tdef __init__(self):\n\t\t\tself.root = TrieNode()\n\n\t\tdef add(self, word: str):\n\t\t\tnode = self.root\n\t\t\tfor char in word:\n\t\t\t\tif char not in node.children:\n\t\t\t\t\tnode.children[char] = TrieNode()\n\t\t\t\tnode = node.children[char]\n\n\t\t\tnode.is_word = True\n\n\t\tdef search(self, word: str, index: int, node=None):\n\t\t\tif node is None: node = self.root\n\t\t\tfor i in range(index, len(word)):\n\t\t\t\tif word[i] == \'.\':\n\t\t\t\t\tfor child in node.children.values():\n\t\t\t\t\t\tif self.search(word, i+1, child):\n\t\t\t\t\t\t\treturn True\n\t\t\t\t\treturn False\n\n\t\t\t\tif word[i] not in node.children:\n\t\t\t\t\treturn False\n\t\t\t\tnode = node.children[word[i]]\n\n\t\t\treturn node.is_word\n\n\n\tclass WordDictionary:\n\t\tdef __init__(self):\n\t\t\tself.trie = Trie()\n\t\t\tself.longest = 0\n\n\t\tdef addWord(self, word: str) -> None:\n\t\t\tself.trie.add(word)\n\t\t\tself.longest = max(self.longest, len(word))\n\n\t\tdef search(self, word: str) -> bool:\n\t\t\tif len(word) > self.longest: return False\n\t\t\treturn self.trie.search(word, 0)\n
['Python3']