leetcode--44. Wildcard matching

Subject: 44. Wildcard matching

Link: https://leetcode-cn.com/problems/wildcard-matching/description/

Given a string ( s) and a character pattern ( ) , implements a  wildcard match pthat supports  '?' sum  .'*'

'?' matches any single character.
'*' can match any string (including the empty string).

This is very similar to the tenth question ( 10. Regular Expression Matching ), the recursion timeout was written, and then it was changed to DP.

python:

class Solution:
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        dp=[[False for col in range(len(p)+1)]for row in range(len(s)+1)]
        dp[0][0]=True
        for i in range(len(s)+1):
            for j in range(1,len(p)+1):
                if p[j-1]=="*":
                    dp[i][j]=dp[i][j-1] or (i>0 and dp[i-1][j])
                else:
                    dp[i][j]=i>0 and dp[i-1][j-1] and (s[i-1]==p[j-1] or p[j-1]=="?")
        return dp[len(s)][len(p)]
        # if not p:
        #     return not s
        # if len(p)==1:
        #     if p[0]=="*":
        #         return True
        #     else:
        #         return len(s)==1 and (s[0]==p[0] or p[0]=="?")
        # if p[0]!="*":
        #     if not s:
        #         return False
        #     else:
        #         return (p[0]==s[0] or p[0]=="?") and self.isMatch(s[1:len(s)],p[1:len(p)])
        # else:
        #     while len(s)!=0:
        #         if self.isMatch(s,p[1:len(p)]):
        #             return True
        # s = s [1: len (s)]
        #     return self.isMatch(s,p[1:len(p)])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324549441&siteId=291194637