题目地址:点击打开链接
class Solution {
public:
bool isMatch(string s, string p) {
return match(s,p,0,0);
}
private:
bool match(string s,string p,int a,int b) {
int m=s.size();
int n=p.size();
if(a==m&&b==n)return true;
if(b==n)return false;
if(b==n-1||p[b+1]!='*'){
if(a<m&&(s[a]==p[b]||p[b]=='.'))return match(s,p,a+1,b+1);
return false;
}else{
if(match(s,p,a,b+2))return true;
while(a<m&&(s[a]==p[b]||p[b]=='.')){
if(match(s,p,a+1,b+2))return true;
++a;
}
return false;
}
}
};