forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0044.cpp
29 lines (26 loc) · 963 Bytes
/
0044.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
class Solution {
public:
bool isMatch(string s, string p) {
const int m = s.size();
const int n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 && j == 0)
dp[i][j] = true;
else if (i == 0)
dp[i][j] = dp[i][j - 1] && p[j - 1] == '*';
else if (j == 0)
dp[i][j] = dp[i - 1][j] && s[i - 1] == '*';
else
dp[i][j] =
(dp[i - 1][j] || dp[i][j - 1] || dp[i - 1][j - 1]) &&
(s[i - 1] == '*' || p[j - 1] == '*') ||
(dp[i - 1][j - 1]) &&
(s[i - 1] == '?' || p[j - 1] == '?' ||
s[i - 1] == p[j - 1]);
}
}
return dp[m][n];
}
};