实现一个支持 '?' 和 '*' 的通配符匹配。
'?' 匹配任何单个字符。
'*' 匹配任何数量的字符 (包括0个)。
匹配应覆盖 整个 输入字符串(而不是部分)。
这个函数原型为:
bool isMatch(const char *s, const char *p)
示例:
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
详见:https://leetcode.com/problems/wildcard-matching/description/

class Solution {
public:
    bool isMatch(string s, string p) {
        int m=s.size();
        int n=p.size();
        vector<vector<int>> dp(m+1,vector<int>(n+1));
        dp[0][0]=true;
        for(int i=1;i<=n;++i)
        {
            if(p[i-1]=='*')
            {
                dp[0][i]=dp[0][i-1];
            }
        }
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(p[j-1]=='*')
                {
                    dp[i][j]=dp[i-1][j]||dp[i][j-1];
                }
                else
                {
                    dp[i][j]=(s[i-1]==p[j-1]||p[j-1]=='?')&&dp[i-1][j-1];
                }
            }
        }
        return dp[m][n];
    }
};

 参考:https://www.cnblogs.com/grandyang/p/4401196.html

相关文章:

  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2021-09-27
  • 2021-10-26
猜你喜欢
  • 2021-11-12
  • 2022-01-05
  • 2022-02-24
  • 2022-12-23
  • 2022-02-05
  • 2022-01-29
  • 2021-11-24
相关资源
相似解决方案