-
Notifications
You must be signed in to change notification settings - Fork 0
/
10 Regular Expression Matching.cpp
69 lines (61 loc) · 1.75 KB
/
10 Regular Expression 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
// unordered<,bool>mp;
bool isMatch(string s, string p) {
// cout<<endl<<endl<<s<<" "<<p<<endl;
int sn=s.length();
int pn=p.length();
if(sn==0){
if(pn==0)
return true;
if(pn==1)
return false;
if(pn>=2){
if(p[pn-1]!='*')
return false;
}
}
if(pn==0 && sn>0)
return false;
--sn;
--pn;
char c=p[pn];
bool ans=false;
if(c=='*'){
char pre=p[pn-1];
ans = ans || isMatch(s.substr(0,sn+1),p.substr(0,pn-1));
if(!ans){
for(int i=0;i<=sn;++i){
// cout<<pre<<"-"<<sn-i<<" ----------- "<<i<<endl;
if(pre=='.'){
ans=ans || isMatch(s.substr(0,sn-i),p.substr(0,pn-1));
}
else if(pre == s[sn-i]){
ans = ans || isMatch(s.substr(0,sn-i),p.substr(0,pn-1));
}
else{
return false;
break;
}
if(ans)
break;
}
}
}
else if(c == '.'){
ans = isMatch(s.substr(0,sn),p.substr(0,pn));
}
else{
if(c == s[sn])
ans = isMatch(s.substr(0,sn),p.substr(0,pn));
}
// cout<<endl;
return ans;
}
};