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 prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

实现通配符,支持‘?‘和’*’。‘?‘匹配任意单个字符,’*’匹配任意字符串序列,包含空字符。匹配整个字符串,而不是部分。

之前做过正则表达式的匹配,与通配符非常类似,不同的是对‘*‘的处理,通配符中用来匹配任意字符序列而不是某个字符的重复。

假设 S 是要匹配的字符串,P 是模式串。还是用DP来解决,设计函数F(i,j)表示S.substr(0, i) 和P.substr(0, j)是否匹配,令M = S.size(),N = P.size(),那么 i ∈ [0, M],j ∈ [0, N],递推公式是 F(i,j) = { P[j]==‘*‘ && (F(i,j-1) || F(i-1,j) } || { (P[j]==‘?‘ || S[i]==P[j]) && F(i-1, j-1) }。左半段表示当P[j]等于‘*‘字符时,有两种情况,一种是F(i,j-1)表示‘*‘匹配零个字符,另一种F(i-1,j)表示匹配一到多个字符;右半段表示当P[j]等于‘?‘或其他字符时,需要检测S[i]和P[j]是否匹配,然后再用F(i-1, j-1)判断之前的串是否匹配。如此,需要计算M*N个状态,如果要存储所有状态,空间复杂度就是是O(M*N),时间复杂度也是O(M*N),不过实际上递推式只用到了相邻的状态,所以可以将二维的空间压缩到一维,例如,可以使用数组R[i]存储S.substr(0,i)与P.substr(0,j)是否匹配,具体做法是:先初始化 j=0, R[0] = true, R[j] = false (j ∈ [1, M]),然后将 j 从 1 迭代到 N ,每次迭代重新计算一次 R ,计算的时候当然会用到上一次的R,最后R[M]就是结果。代码如下:

bool isMatch(string s, string p) {
    vector<bool> bM(s.size()+1, false);
    bM[0] = true;
    for (int j=0; j<p.size(); ++j)
    {
        bool last = bM[0];
        if (p[j] != ‘*‘)
            bM[0] = false;
        for (int i=0; i<s.size(); ++i)
        {
            bool tmp = bM[i+1];
            if (‘*‘ == p[j])
                bM[i+1] = bM[i] || bM[i+1];
            else
                bM[i+1] = (‘?‘ == p[j] || s[i]==p[j]) && last;
            last = tmp;
        }
    }
    return bM.back();
}
时间: 2024-08-05 14:51:25

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 (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).

第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧.. class Solution { public: bool match(string &s,string &p,int l2,int r2,int l1) { if(l2==r2)return true; if(l1+r2-l2-1>=s.length())re