7-liner Python DP

  • 0
        def isMatch(self, s, p):
            dp = [[False]*(len(p)+1) for j in range(len(s)+1)]
            dp[0][0] = True
            for i in range(0, len(s)+1):
                for j in range(1, len(p)+1):
                    dp[i][j] = dp[i][j-2] or i>0 and (s[i-1] == p[j-2] or p[j-2] == '.') and dp[i-1][j] \
                    if p[j-1] == '*' else i>0 and dp[i-1][j-1] and (s[i-1] == p[j-1] or p[j-1] == '.')
            return dp[-1][-1]

Log in to reply

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