Try Again

leetcode44通配符匹配

class Solution {
public:
    bool isMatch(string s, string p) {
        int n = s.size(), m = p.size();
        int dp[n + 2][m + 2];
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for(int i = 1; i <= m; i++)
            if(p[i - 1] == '*')dp[0][i] = dp[0][i - 1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(s[i - 1] == p[j - 1] || p[j - 1] == '?') dp[i][j] = dp[i - 1][j - 1];
                else if(p[j - 1] == '*') dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
            }
        }
        return dp[n][m];
    }
};
posted @ 2021-10-31 16:34  十年换你一句好久不见  阅读(20)  评论(0编辑  收藏  举报