-
Notifications
You must be signed in to change notification settings - Fork 0
/
44. Wildcard Matching.cpp
40 lines (39 loc) · 1.08 KB
/
44. Wildcard Matching.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
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
bool solve(string &s,string &p,int i,int j,int &m,int &n,vector<vector<int>> &v){
if(i==m&&j==n)return true;
if(i == m){
for(int k = j; k < n; k++){
if(p[k] != '*')return false;
}
return true;
}
if(j == n && i != m)return false;
if(v[i][j] != -1)return v[i][j];
bool ans = false;
if(p[j]==s[i]){
if(solve(s,p,i+1,j+1,m,n,v))
ans = true;
}
if(p[j]== '?'){
if(solve(s,p,i+1,j+1,m,n,v))
ans= true;
}
else if(p[j] == '*'){
if(solve(s,p,i+1,j+1,m,n,v)){
ans = true;
}if(solve(s,p,i+1,j,m,n,v)){
ans = true;
}if(solve(s,p,i,j+1,m,n,v)){
ans = true;
}
}
v[i][j] = ans;
return ans;
}
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<int>> v(m,vector<int>(n,-1));
return solve(s,p,0,0,m,n,v);
}
};