-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_10.cpp
More file actions
29 lines (27 loc) · 802 Bytes
/
Copy pathleetcode_10.cpp
File metadata and controls
29 lines (27 loc) · 802 Bytes
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:
int cache[1000][1000];
bool isMatch(string s, string p) {
memset(cache, -1, sizeof(cache));
return match(0, 0, s, p);
}
bool match(int i, int j, const string& s, const string& p){
if(cache[i][j] != -1)
return cache[i][j];
bool ans;
if(j == p.size()){
ans = i == s.size();
}
else {
bool first_match = (i < s.size() && (s[i] == p[j] || p[j] =='.'));
if(j + 1 < p.size() && p[j+1] == '*'){
ans = match(i, j+2, s, p) || (first_match && match(i+1, j, s, p));
}
else {
ans = first_match && match(i+1, j+1, s, p);
}
}
cache[i][j] = ans ? 1 : 0;
return ans;
}
};