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)

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

1. 动态规划
bool isMatch(string s, string p) {
    int ls = s.length(), lp = p.length(), i, j;
    vector<vector<bool>> dp(2, vector<bool>(lp+1, 0));
    bool k = 1;
    dp[0][0] = 1;
    for(i = 1; i <= lp; i++)
        dp[0][i] = dp[0][i-1] && ‘*‘ == p[i-1];
    for(i = 1; i <= ls; i++)
    {
        dp[k][0] = 0;
        for(j = 1; j <= lp; j++)
        {
            if(‘*‘ == p[j-1])
                dp[k][j] = dp[k][j-1] || dp[!k][j];
            else
                dp[k][j] = dp[!k][j-1] && (p[j-1] == s[i-1] || ‘?‘ == p[j-1]);
        }
        k = !k;
    }
    return dp[!k][lp];
}

2. 不匹配的时候回到上一个星号的地方,使星号多匹配一个字符。

bool isMatch(string s, string p) {
    int ls = s.length(), lp = p.length(), last_i = -1, last_j = -1, i = 0, j = 0;
    while(s[i])
    {
        if(‘*‘ == p[j])
        {
            j++;
            if(!p[j])
                return 1;
            last_i = i;
            last_j = j;
        }
        else if(s[i] == p[j] || ‘?‘ == p[j])
        {
            i++;
            j++;
        }
        else if(last_i != -1)
        {
            i = ++last_i;
            j = last_j;
        }
        else
            return 0;
    }
    while(‘*‘ == p[j])
        j++;
    return !p[j];
}
时间: 2024-12-21 01:21:23

44. Wildcard Matching *HARD*的相关文章

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

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