[Python] Solution gets weird run time error in OJ.


  • 0
    E
    class Solution:
        # @param s, an input string
        # @param p, a pattern string
        # @return a boolean
        def isMatch(self, s, p):
            def isMatchHelper(s, p, lft, rgt, memo):
                if (lft,rgt) in memo: return False      # memoization
                if rgt == len(p):                                # pattern exhausted
                    if lft == len(s): return True           # string exhausted
                elif p[rgt] == '*':
                    for idx in range(lft, len(s)+1):      # match all possible left-substrings
                        if isMatchHelper(s, p, idx, rgt+1, memo): return True
                elif lft < len(s) and p[rgt] in set(['?', s[lft]]):  # match next element
                           if isMatchHelper(s, p, lft+1, rgt+1, memo): return True
                memo.add((lft,rgt))                           # didn't match 
                return False
            memo = set()           
            return isMatchHelper(s, p, 0, 0, memo)
    

    I get a runtime-error: Last expected input "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*"

    Does anyone have any idea what is going on? It runs fine on my laptop.


Log in to reply
 

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