[Python] Why TLE

  • 0

    My solution thought is similar to leetcode
    It runs well at my machine.
    WHY I get a TLE against case "aaaaaaaaaaaaab", "aaaaaaaaaac"

    class Solution():
        def isMatch(self, s, p):
            if len(s) == 0 and len(p) == 0:
                return True
            if len(p) == 0:
                return False
            if len(s) == 0:
                return self.isMatch(s, p[2:]) if len(p)>2 and p[1] == '*' else False
            if len(p)>2 and p[1] == '*':
                return (s[0] == p[0] and self.isMatch(s[1:], p[:])) or self.isMatch(s[:], p[2:])
            elif p[0] == '.' or p[0] == s[0]:
                return self.isMatch(s[1:], p[1:])
            return False

Log in to reply

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