TLE in Java but Accepted in C++?


  • 0
    X

    Hi All,

    I submit my code in java but I got a TLE and I submit the same algorithm in C++ and I got accepted, so anyone has some idea about it?


  • 0

    Could you please edit your question and paste both of your C++ and Java code, so we can take a look at it?


  • 0
    T

    I wrote in python, same problem as yours.

    class Solution:
        # @return a boolean
        def isMatch(self, s, p):
            return self.helpMatch(s,p,0,0)
            
        def helpMatch(self,s,p,sp,pp):
            if pp==len(p) and sp==len(s):
                return True
            if pp>=len(p) or sp>=len(s):
                return False
            if pp==len(p)-1 or p[pp+1]!='*':
                if p[pp]=='.' or s[sp]==p[pp]:
                    return self.helpMatch(s,p,sp+1,pp+1)
                else:
                    return False
            else:
                while (sp)<len(s) and (p[pp]=='.' or s[sp]==p[pp]):
                    if self.helpMatch(s,p,sp,pp+2):
                        return True
                    sp=sp+1
                return self.helpMatch(s,p,sp,pp+2)
    

    I think the algorithm is pretty much the same as the c++ ones here http://leetcode.com/2011/09/regular-expression-matching.html. But TLE in python.


Log in to reply
 

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