Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:
Input: s = "aa", p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
class Solution {
public:
bool isMatch(string s, string p) {
vector<vector<int>> dp(s.length()+1,vector<int>(p.length(),-1));
return helper(s,p,0,0,dp);
}
bool helper(string s, string p, int i, int j,vector<vector<int>> &dp)
{
if(j==p.length())
return i==s.length();
if(dp[i][j]>=0)
return dp[i][j];
bool first_match=(i<s.length() && (s[i]==p[j] || p[j]=='.' ));
bool ans=0;
if(j+1<p.length() && p[j+1]=='*')
{
ans= (helper(s,p,i,j+2,dp)|| (first_match && helper(s,p,i+1,j,dp) ));
}
else
{
ans= (first_match && helper(s,p,i+1,j+1,dp));
}
dp[i][j]=ans;
return ans;
}
};