forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
27 lines (27 loc) · 830 Bytes
/
Solution.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
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
int f[m + 1][n + 1];
memset(f, 0, sizeof f);
function<bool(int, int)> dfs = [&](int i, int j) -> bool {
if (j >= n) {
return i == m;
}
if (f[i][j]) {
return f[i][j] == 1;
}
int res = -1;
if (j + 1 < n && p[j + 1] == '*') {
if (dfs(i, j + 2) or (i < m and (s[i] == p[j] or p[j] == '.') and dfs(i + 1, j))) {
res = 1;
}
} else if (i < m and (s[i] == p[j] or p[j] == '.') and dfs(i + 1, j + 1)) {
res = 1;
}
f[i][j] = res;
return res == 1;
};
return dfs(0, 0);
}
};