leetcode-44. Wildcard Matching

这道题和之前的那道Regular Expression Matching有点相似,第一反应是跟之前一样,用递归来做

bool doMatch(char *s,char *p)
{
    if (*p == ‘\0‘)    return *s == ‘\0‘;
    if(*p == ‘*‘)
    {
        while(*s!=‘\0‘)//通配符匹配0次,1次····
        {
            if(doMatch(s,p+1))
                return true;
            s++;
        }
        //通配符匹配完后还是无法匹配
        return doMatch(s,p+1);
    }
    else
    {
  //      if(*p!=*s && *p!=‘.‘)   return false;
  //      return doMatch(s+1,p+1);
        if (*p == *s || (*p == ‘?‘ && *s != ‘\0‘))
        return doMatch(s+1, p+1);
        return false;  

    }
}
bool isMatch(char* s, char* p) {
    if(*p==‘\0‘)    return *s==‘\0‘;
    char *new_p = (char *)malloc(strlen(p));
    int j=0;
    //先对p预处理,多个*变为一个
    for(int i=0;i<strlen(p);i++)
    {
        if(i>0 && p[i]==‘*‘ && p[i-1]==‘*‘)
            continue;
        new_p[j] = p[i];
        j++;
    }
    new_p[j] = ‘\0‘;
    return doMatch(s,new_p);
}

超时了。。。这是因为出现了重复比较的情况。比如当p = "c*ab*c",s = "cddabbac",遇到第一个*时,需要用递归处理p的剩余部分"ab*c" 和s的剩余部分"ddabbac"。遇到第二个*时还需要比较剩余部分,所以需要用动态规划消除重复子问题。

dp[i][j]代表到p[i-1]和s[j-1]为止一直匹配,所以当前字符为*时dp[i][j]=dp[i-1][j]||dp[i][j-1]||dp[i-1][j-1]

当前字符为其他情况时 dp[i]pj]=dp[i-1][j-1] && (p[i-1]==‘?‘ || p[i-1]==s[j-1])

这里为了方便设初值,增加一行一列,并令dp[0][0]=true;

我们注意到dp的值只与前一行和前一列有关,所以可以将数组缩减为两行以节省空间。

class Solution {
public:
    bool isMatch(string s, string p) {
        int s_len = s.length();
        int p_len = p.length();
        vector<vector<bool>>dp(2,vector<bool>(s_len+1,false));
        dp[0][0] = 1;
        for(int i=1;i<=p_len;i++)
        {
            int cur=i%2;
            int prev=(i+1)%2;
            dp[cur][0]=dp[prev][0]&&p[i-1]==‘*‘;
            for(int j=1;j<=s_len;j++)
            {
                if(p[i-1]==‘*‘)
                    dp[cur][j] = dp[cur][j-1] || dp[prev][j] ||dp[prev][j-1];
                else
                    dp[cur][j] = dp[prev][j-1] && (p[i-1]==‘?‘ || p[i-1]==s[j-1]);
            }
        }
        return dp[p_len%2][s_len];
    }
};
时间: 2024-12-16 18:49:23

leetcode-44. Wildcard Matching的相关文章

第八周 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

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,

(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

LeetCode 44 Wildcard Matching(字符串匹配问题)

题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). 字符串匹配问题. 如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串 给定字符串s,和字符串p.判

[动态规划] leetcode 44 Wildcard Matching

problem:https://leetcode.com/problems/wildcard-matching/ 用记忆化搜索做的,不容易出错: class Solution { public: vector<vector<int>> dp; bool isMatch(const string& s, const string& p, int i, int j) { if(i >= s.size() && j >= p.size()) r

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】Wildcard Matching

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 partia

[LeetCode][JavaScript]Wildcard Matching

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 partia

【LeetCode】Wildcard Matching 串匹配 动态规划

题目:Wildcard Matching <span style="font-size:18px;">/*LeetCode WildCard matching * 题目:给定一个目标串和一个匹配串,判定是否能够匹配 * 匹配串中的定义:字符---->字符,*---->0个.1个或者多个字符,?------>对应任意一个字符 * 思路:动态规划:*:dp[i][j] = dp[i][j-1] || dp[i-1][j] * ? || s[i] == p[i]