DP solution c++ clean code


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.