Just another python solution with recursion

  • 0
    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]) 
                    return self.isMatch(s, p[:-2])
                if s and (p[-1] == s[-1] or p[-1] == '.'):
                    return self.isMatch(s[:-1], p[:-1])
                    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.