-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathWildcard_Matching.cpp
33 lines (32 loc) · 1.02 KB
/
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
// 44. Wildcard Matching
class Solution {
public:
bool isMatch(string &s, string &p) {
int is=s.size(), ip=p.size();
bool dp[is+1][ip+1];
for(int i=0;i<=is;++i){
for(int j=0;j<=ip;++j){
if(i==0 and j==0){
dp[i][j] = true;
}else if(i==0){
if(p[j-1]=='*') dp[i][j] = dp[i][j-1];
else dp[i][j] = false;
}else if(j==0){
dp[i][j] = false;
}else{
char curS = s[i-1], curP = p[j-1];
if(curS==curP){
dp[i][j] = dp[i-1][j-1];
}else if(curP=='?'){
dp[i][j] = dp[i-1][j-1];
}else if(curP=='*'){
dp[i][j] = (dp[i][j-1] or dp[i-1][j-1] or dp[i-1][j]);
}else{
dp[i][j] = false;
}
}
}
}
return dp[is][ip];
}
};