classSolution{public:boolresult;boolisMatch(constchar*s,constchar*p){// Note: The Solution object is instantiated only once and is reused by each test case.
result=false;intlens=strlen(s);intlenp=strlen(p);if(p[lenp-1]!='*'&&s[lens-1]!=p[lenp-1]&&p[lenp-1]!='.')returnresult;matchStep(s,p);returnresult;}private:voidmatchStep(constchar*s,constchar*p){if(result)return;if(*s=='\0'&&*p=='\0'){result=true;return;}if(*s=='\0'){if(*(p+1)=='*'){matchStep(s,p+2);return;}}if(*s=='\0'||*p=='\0')return;if(*(p+1)!='\0'){if(*(p+1)!='*'){if(*s==*p||*p=='.'){matchStep(s+1,p+1);}elsereturn;}else{if(*s!=*p&&*p!='.'){matchStep(s,p+2);}else{matchStep(s,p+2);matchStep(s+1,p);matchStep(s+1,p+2);}}}else{if(*s==*p||*p=='.'){matchStep(s+1,p+1);}elsereturn;}}};