Just another python solution with recursion


  • 0
    E
    class Solution:
        # @param {string} s
        # @param {string} p
        # @return {boolean}
        def isMatch(self, s, p):
            if not s and not p:
                return True
    
            if not p and s:
                return False
    
            if p[-1] == '*':
                rep = p[-2]
                if s and (s[-1] == rep or rep == '.'):
                    return self.isMatch(s[:-1], p) or self.isMatch(s, p[:-2]) 
                else:
                    return self.isMatch(s, p[:-2])
            else:
                if s and (p[-1] == s[-1] or p[-1] == '.'):
                    return self.isMatch(s[:-1], p[:-1])
                else:
                    return False
    

    Might be a little easier to read. 181 ms cost.


Log in to reply
 

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