python leetcode 44. Wildcard Matching

与正则匹配相同

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        lp = len(p)
        
        ls = len(s)
        dp=[[0]*(lp+1) for i in range(ls+1)]
        dp[0][0]=1
        for i in range(lp):
            if p[i]=='*' and dp[0][i] == 1:
                dp[0][i+1] = 1 
        
        for i in range(ls):
            for j in range(lp):
                if p[j] == s[i] or p[j]=='?':
                    dp[i+1][j+1]=dp[i][j]
                if p[j]=="*":
                    dp[i+1][j+1] = dp[i+1][j] or dp[i][j+1] or dp[i][j] 
        return dp[ls][lp] == 1

你可能感兴趣的:(leetcode,python)