1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: bool isMatch(string s, string p) { int m = s.length(), n = p.length(); cache.resize(m + 1, vector<int>(n + 1, -1)); return f(m, n, s, p); }
bool f(int i, int j, const string &s, const string &p) { if (i == 0 && j == 0) return true; if (i > 0 && j < 1) return false; if (cache[i][j] != -1) return cache[i][j]; if (i == 0) return cache[i][j] = p[j - 1] == '*' && f(i, j - 1, s, p); if (p[j - 1] == '*') return cache[i][j] = f(i, j - 1, s, p) || f(i - 1, j, s, p); return cache[i][j] = (s[i - 1] == p[j - 1] || p[j - 1] == '?') && f(i - 1, j - 1, s, p); }
vector<vector<int>> cache; };
|