__author__ = 'ZhongH'
class Trie:
def __init__(self):
self.son = [0] * 27
self.c = '\0'
self.sonVal = 0
class WordDictionary:
def __init__(self):
self.root = Trie()
def addWord(self, word):
p = self.root
for c in word:
x = ord(c)  ord('a')
if p.son[x] == 0:
q = Trie()
q.c = c
p.son[x] = q
p = p.son[x]
q = Trie()
p.son[26] = q
p.isword = True
def search(self, word):
tmp = p = self.root
res = False
if word == '':
if self.root.son[26] != 0:return True
else:return False
elif word[0] == '.':
i = 0
while i < 26:
if p.son[i]!=0:
self.root = p.son[i]
if self.search(word[1:]):
self.root = tmp
return True
i+=1
else:
x = ord(word[0])  ord('a')
if p.son[x] == 0:return False
self.root = p.son[x]
if self.search(word[1:]):
self.root = tmp
return True
Why it is always time limited out with the python code ,whereas c not?


I want to ask the same question, my python code is essentially the same with the C++ solution https://leetcode.com/discuss/35949/csimplesolution (i.e. no complexity improvement), but it encountered TLE. This is a new problem, maybe someone can take a look at the python test cases and time constraints?