Untitled
class Solution { public: bool solve(int i,int j,string &s, string &p){ if(i>=s.length() && j>=p.length()) return true; if(j>=p.length()) return false; //we have exhausted p but still not matched so false //if(p[j]!='.' && p[j]!='*' && p[j]!=p[i]) return false; if(j+1<p.length() && p[j+1]=='*'){ bool inc=false; if((p[j]=='.' || s[i]==p[j]) && i<s.length()) inc=solve(i+1,j,s,p); bool exc=solve(i,j+2,s,p); return inc||exc; } if (i<s.length() &&(p[j]=='.' || p[j]==s[i])) return solve(i+1,j+1,s,p); //current index tru check for next return false; } bool isMatch(string s, string p) { return solve(0,0,s,p); } };
Leave a Comment