-
Notifications
You must be signed in to change notification settings - Fork 3
/
44.cpp
30 lines (30 loc) · 1.17 KB
/
44.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int sSize;
int pSize;
vector<vector<int>> memo;
int _isMatch(string& s, string& p, int sIdx, int pIdx) {
if (memo[sIdx][pIdx] != -1) return memo[sIdx][pIdx];
if (pIdx == pSize - 1 && p[pIdx] == '*') return memo[sIdx][pIdx] = 1;
if (sIdx == sSize && pIdx == pSize) return memo[sIdx][pIdx] = 1;
if (sIdx < sSize && pIdx == pSize) return memo[sIdx][pIdx] = 0;
if (p[pIdx] == '?') {
if (sIdx == sSize) return memo[sIdx][pIdx] = 0;
return memo[sIdx][pIdx] = _isMatch(s, p, sIdx + 1, pIdx + 1);
}
else if (p[pIdx] == '*') {
if (sIdx < sSize) return memo[sIdx][pIdx] = _isMatch(s, p, sIdx + 1, pIdx) || _isMatch(s, p, sIdx, pIdx + 1);
return memo[sIdx][pIdx] = _isMatch(s, p, sIdx, pIdx + 1);
}
else {
if (s[sIdx] != p[pIdx]) return false;
return memo[sIdx][pIdx] = _isMatch(s, p, sIdx + 1, pIdx + 1);
}
}
bool isMatch(string s, string p) {
sSize = s.size();
pSize = p.size();
memo.resize(sSize + 1, vector<int>(pSize + 1, -1));
return _isMatch(s, p, 0, 0);
}
};