蠡口44. Wildcard Matching

两个字符串,比较后返回T or F,题目很像10 Regular Expression Matching。一样使用动态规划只是在状态转移的时候有点tricky。

1、建立dp矩阵:dp[i][j]: if s[0:i] matches p[0:j], 注意字符串s[0:i]不包括s[i],所以dp矩阵的size应该为(lens+1)*(lenp+1),初始值全为F;

2、初始化边界条件:dp[0][0]表示""(空字符)与""(空字符)是否match,当然是True。dp[i][0] (i>0)必然是False,因为一个空字符pattern p不可能match一个非空字符s。然而,一个非空的pattern可能match一个空字符,因为pattern中的“*”可以match空字符,所以当dp[0][j-1] (p里前j-1个字符match空字符)且dp[j-1]为“*”时dp[0][j]为True;

3、状态转移方程:当考虑dp[i][j]时,p[j-1]可能取以下四种值:

  1)p[j-1]==s[i-1], 那么这时候只有当dp[i-1][j-1]为T时,dp[i][j]才可能是T。因为这时候就算dp[i][j-1]为T,p[j-1]不为"*", 不能match一个空字符;就算dp[i-1][j]为T,我们在确定边界时提到了,s中的任一字符都不能匹配一个空字符;

  2)p[j-1]=="?", 由于"?"可以匹配s中的任一字符,只有当"?"匹配了s[i-1]时,dp[i][j]才有可能为T。所以此时情况相当于1);

  3)p[j-1]不为s[i-1],"?","*"中的任一一个,那么dp[i][j]肯定为F,不用做任何处理;

  4)p[j-1]=="*", 这时候如果存在dp[k][j-1](ki)为T,那么dp[i][j]也为T。因为"*"可以匹配s[(k+1):i]。使用一个循环来判断是没有错的,但是会超时。我一开始也是这样做的,无赖只能请教网上的大神们。于是发现一个tricky:if p[j]=="*" and dp[i-1][j]==True   <=>    there is a k(<i), such that dp[k][j-1]==True!为了理解更直观,我画了个简易图来表示dp[k][j-1], dp[i-1][j]dp[i][j]

  一开始我是不信这个邪的,我们一起来证明一下:

  <=) 若dp[k][j-1]为T那么s[0:k]可以匹配p[0:(j-1)];再加上s[k:(i-1)]可以与"*"(p[j-1])匹配,所以s[0:(i-1)]可以与p[0:j]匹配,即dp[i-1][j]=T;

  =>)反证法,假设对于所有k(k<i), dp[k][j-1]都为F。那么不管p[j-1]这个"*"与哪个s[x:(i-1)]匹配,由于dp[x][j-1]为F,dp[i-1][j]都为F,矛盾!

4、返回值:dp[lens][lenp]: if s[0:lens] matches p[0:lenp]

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        #dp[]
        if len(p)==0: return(len(s)==0)
        lens,lenp=len(s),len(p)
        #dp[i][j]: if s[0:i] matches p[0:j]
        dp=[[False for i in range(lenp+1)] for j in range(lens+1)]
        dp[0][0]=True
        for j in range(1,lenp+1):
            if p[j-1]=="*" and dp[0][j-1]: dp[0][j]=True
        #print(dp)
        for i in range(1,lens+1):
            for j in range(1,lenp+1):
                #print(dp[i][j])
                if p[j-1]==s[i-1] or p[j-1]=="?": dp[i][j]=dp[i-1][j-1]
                elif p[j-1]=="*": dp[i][j]=dp[i-1][j] or dp[i][j-1]
                #Note that if p[j]=="*" and dp[i-1][j]==True   <=>    there is a k(<i), such that dp[k][j-1]==True
        #print(dp)
        return(dp[lens][lenp])

原文地址:https://www.cnblogs.com/Leisgo/p/11703419.html

时间: 2024-10-11 16:27:11

蠡口44. Wildcard Matching的相关文章

44. Wildcard Matching(js)

44. Wildcard Matching Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

44. Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

44. Wildcard Matching (String; Recursion)

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence).不同于正则表达式中的* *正则表达式的定义: '.' Matches any single character. '*' Matches zero or mor

【一天一道LeetCode】#44. Wildcard Matching

一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not par

(Java) LeetCode 44. Wildcard Matching —— 通配符匹配

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the enti

19.2.4 [LeetCode 44] Wildcard Matching

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the enti

44. Wildcard Matching *HARD*

'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Som

10. Regular Expression Matching &amp;&amp; 44. Wildcard Matching

10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial).