<p>Given an input string <code>s</code>&nbsp;and a pattern <code>p</code>, implement regular expression matching with support for <code>&#39;.&#39;</code> and <code>&#39;*&#39;</code> where:</p>

<ul>
	<li><code>&#39;.&#39;</code> Matches any single character.​​​​</li>
	<li><code>&#39;*&#39;</code> Matches zero or more of the preceding element.</li>
</ul>

<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aa&quot;, p = &quot;a&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> &quot;a&quot; does not match the entire string &quot;aa&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aa&quot;, p = &quot;a*&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> &#39;*&#39; means zero or more of the preceding element, &#39;a&#39;. Therefore, by repeating &#39;a&#39; once, it becomes &quot;aa&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;ab&quot;, p = &quot;.*&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> &quot;.*&quot; means &quot;zero or more (*) of any character (.)&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length&nbsp;&lt;= 20</code></li>
	<li><code>1 &lt;= p.length&nbsp;&lt;= 20</code></li>
	<li><code>s</code> contains only lowercase English letters.</li>
	<li><code>p</code> contains only lowercase English letters, <code>&#39;.&#39;</code>, and&nbsp;<code>&#39;*&#39;</code>.</li>
	<li>It is guaranteed for each appearance of the character <code>&#39;*&#39;</code>, there will be a previous valid character to match.</li>
</ul>

class Solution {
public:
    bool isMatch(string A, string B) {
        vector<string> s;
        vector<string> t;

        /*
            Simply consider each character as string:
                A = vidit

            s = { "v", "i", "d", "i", "t"}

        */

        for(int i=0;i<A.length();i++){
            string x = "";
            x+=A[i];
            s.push_back(x);
        }

        /*
            Since * can only be appended to a character or a '.'
            We consider a*, b*, c*, .* etc to be single characters.

            B = ab*cc.*
            to be considered as:

            t = { "a", "b*", "c", "c", ".*" } 

        */

        for(int i=0;i<B.length();i++){
            if(B[i]=='*'){
                t[t.size()-1]= t[t.size()-1] + "*";
            }else{
                string x = "";
                x+=B[i];
                t.push_back(x);
            }
        }

        /*
            dp[i][j]: if prefix till i in s and till j in t match. 
                where i starts from 0 signifying empty prefix of s
                and j starts from 0 signifying emptiy prefix of t
        */

        int n = s.size();
        int m = t.size();
        
        vector<vector<bool>> dp(n+1,vector<bool>(m+1,false));

        //BASE CASES

        dp[0][0] = true;

        for(int i=1;i<=n;i++){
            dp[i][0] = false;
        }

        bool charencounteredtillJ = false;
        for(int j=1;j<=m;j++){
            if(t[j-1].size()==1){
                charencounteredtillJ = true;
            }
            dp[0][j] = (charencounteredtillJ==false);
        }

        //REAL DP

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                //NOTE: i,j in dp corresponds to s[i-1], t[j-1]
                /* 
                    means we have a character like "a"
                */
                if(s[i-1]==t[j-1] && dp[i-1][j-1])
                    dp[i][j]=true;
                /*
                    "." can match a single character. 
                */
                if(t[j-1]=="." && dp[i-1][j-1])
                    dp[i][j]=true;
                /*
                    means we have a ".*" or "a*" in t and we want it to take value empty. 
                */
                if(t[j-1].length()==2 && dp[i][j-1])
                    dp[i][j]=true;			
                /*
                    means we have a "a*" in t and we want it to take the value {a, aa, aaa,...} to match in s. 
                    Note dp[i-1][j] true necessarily implies dp[i-2][j] to be true in this case.
                */
                if(t[j-1].length()==2 && dp[i-1][j] && (t[j-1][0]==s[i-1][0]))
                    dp[i][j]=true;	
                /*
                    means we have a ".*" in t and we want to use it to match {single, multiple characters}. 
                    Note dp[i-1][j] true necessarily implies dp[i-2][j] to be true in this case.
                */
                if(t[j-1].length()==2 && (t[j-1][0]=='.') && dp[i-1][j])
                    dp[i][j]=true;				
            }
        }
	    return dp[n][m];
    }
};

11243