<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:
    struct pair_hash {
        template <class T1, class T2>
        std::size_t operator() (const std::pair<T1, T2>& pair) const {
            return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
        }
    };

    bool helper(string& s, string& p, size_t sidx, size_t pidx, size_t level, std::unordered_map<std::pair<size_t, size_t>, bool, pair_hash>& memory)
    {
        //std::cout << "L" << level << " " << sidx << " " << pidx << " " << std::endl;
        size_t orig_sidx = sidx;
        size_t orig_pidx = pidx;
        if (memory.find({sidx, pidx}) != memory.end())
        {
            return memory[{sidx, pidx}];
        }
        if (sidx >= s.length() && pidx >= p.length())
        {
            //std::cout << "true" << std::endl;
            memory[{orig_sidx, orig_pidx}] = true;
            return true;
        }
        
        
        for (; pidx < p.length(); pidx += 1)
        {
            //std::cout << pidx << std::endl;
            if (p[pidx] != '*')
            {
                if (p[pidx] != '.')
                {
                    if (pidx+1 < p.length() && p[pidx+1] == '*')
                    {
                        // a letter followed by *
                        size_t i = 0;
                        for ( ; sidx + i <= s.length(); i++)
                        {
                            if (i != 0 && s[sidx+i-1] != p[pidx])
                            {
                                break;
                            }
                            if (helper(s, p, sidx+i, pidx+2, level+1, memory))
                            {
                                //std::cout << "true" << std::endl;
                                memory[{orig_sidx, orig_pidx}] = true;
                                return true;
                            }
                        }
                        bool tmp = helper(s, p, sidx+i-1, pidx+2, level+1, memory);
                        memory[{orig_sidx, orig_pidx}] = tmp;
                        return tmp;
                    }
                    else
                    {
                        // a letter not followed by *
                        if (sidx >= s.length() || p[pidx] != s[sidx])
                        {
                            //std::cout << "false" << std::endl;
                            memory[{orig_sidx, orig_pidx}] = false;
                            return false;
                        }
                        sidx += 1;
                    }
                }
                else
                {
                    if (pidx+1 < p.length() && p[pidx+1] == '*')
                    {
                        // a . followed by *
                        for (size_t i = 0 ; sidx + i <= s.length(); i++)
                        {
                            if (helper(s, p, sidx+i, pidx+2, level+1, memory))
                            {
                                //std::cout << "true" << std::endl;
                                memory[{orig_sidx, orig_pidx}] = true;
                                return true;
                            }
                        }
                        //std::cout << "false" << std::endl;
                        memory[{orig_sidx, orig_pidx}] = false;
                        return false;
                    }
                    else
                    {
                        // a letter not followed by *
                        if (sidx >= s.length())
                        {
                            memory[{orig_sidx, orig_pidx}] = false;
                            return false;
                        }
                        sidx += 1;
                    }
                }
            }
        }
        //std::cout << "false" << std::endl;
        bool tmp = (sidx >= s.length() && pidx >= p.length());
        memory[{orig_sidx, orig_pidx}] = tmp;
        return tmp;
    }

    bool isMatch(string s, string p) {
        std::unordered_map<std::pair<size_t, size_t>, bool, pair_hash> memory;
        return helper(s, p, 0, 0, 0, memory);
    }
};
13493