-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
50 lines (45 loc) · 1.52 KB
/
solution.h
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size();
int m = p.size();
vector<vector<int>> dp(n + 2, vector<int>(m + 2, -1));
function<bool(int, int)> dfs = [&](int l1, int l2) -> bool {
if (l1 > n && l2 > m) return true;
if (l2 > m) return false;
if (dp[l1][l2] != -1) return dp[l1][l2];
if (l2 < m && p[l2] == '*') {
char it = p[l2 - 1];
bool flag = dfs(l1, l2 + 2);
if (flag) return dp[l1][l2] = true;
for (int i = l1 - 1; i < n; i++) {
if (s[i] == it || it == '.') {
flag = dfs(i + 2, l2 + 2);
if (flag) return dp[l1][l2] = true;
} else {
break;
}
}
return dp[l1][l2] = false;
} else {
if (l1 > n) return false;
if (s[l1 - 1] == p[l2 - 1] || p[l2 - 1] == '.') {
return dp[l1][l2] = dfs(l1 + 1, l2 + 1);
} else {
return dp[l1][l2] = false;
}
}
};
return dfs(1, 1);
}
};