-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem10.cpp
More file actions
28 lines (22 loc) · 838 Bytes
/
problem10.cpp
File metadata and controls
28 lines (22 loc) · 838 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
class Solution {
public:
vector<vector<int>> dp;
bool solve ( int i , int j , string s , string p ) {
if (dp[i][j] != -1)
return dp[i][j];
if ( j >= p.length() ) {
return dp[i][j] = (i == s.length());
}
bool matched = ( i < s.length() && (s[i]==p[j] || p[j] == '.'));
if ( j+1 < p.length() && p[j+1] == '*') {
bool not_take = j+2 <= p.length() && solve(i , j+2 , s , p );
bool take = matched && solve(i+1 , j , s , p );
return dp[i][j] = (take || not_take);
}
return dp[i][j] = (matched && solve ( i+1 , j+1 , s , p ));
}
bool isMatch(string s, string p) {
dp.assign(s.length() + 1, vector<int>(p.length() + 1, -1));
return solve(0 , 0 , s , p );
}
};