Why it is always time limited out with the python code ,whereas c not?

  • 0
    __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
                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

  • 0

    I want to ask the same question, my python code is essentially the same with the C++ solution https://leetcode.com/discuss/35949/c-simple-solution (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?

  • 0

    Thanks for the feedback, I've extended the time limit for Python. @hlove, your program now produces a Wrong Answer, and @jerryzh, your program should AC now.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.