<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 {
private:
    enum Instruction
    {
        ANYREPEAT,
        CHARREPEAT,
        CHAR,
        ANY
    };

    struct InstructionStep
    {
        Instruction type;
        char val;
    };

    union InputHash
    {
        struct
        {
            int stringIndex;
            int stepIndex;
        };
        long hash;
    };

    unordered_map<long, bool> _cache;

    bool Recurse(const string& s, const string& p, const vector<InstructionStep>& instructions, int stringIndex = 0, int stepIndex = 0)
    {
        InputHash hash;
        hash.stringIndex = stringIndex;
        hash.stepIndex = stepIndex;
        auto cacheResult = _cache.find(hash.hash);
        if (cacheResult != _cache.end())
        {
            return (*cacheResult).second;
        }

        for (; stepIndex < instructions.size(); stepIndex++)
        {
            InstructionStep step = instructions[stepIndex];
            switch (step.type)
            {
                case CHAR:
                    if (stringIndex > s.length() || s[stringIndex++] != step.val)
                    {
                        _cache[hash.hash] = false;
                        return false;
                    }
                    break;
                case ANY:
                    stringIndex++;
                    break;
                case CHARREPEAT:
                    for (; stringIndex < s.length(); stringIndex++)
                    {
                        if (Recurse(s, p, instructions, stringIndex, stepIndex + 1))
                            break;
                        if (s[stringIndex] != step.val)
                        {
                            _cache[hash.hash] = false;
                            return false;
                        }
                    }
                    break;
                case ANYREPEAT:
                    for (; stringIndex < s.length(); stringIndex++)
                    {
                        if (Recurse(s, p, instructions, stringIndex, stepIndex + 1))
                        {
                            _cache[hash.hash] = true;
                            return true;
                        }
                    }
                    break;
            }
        }
        auto result = stringIndex == s.length();
        _cache[hash.hash] = result;
        return result;
    }

public:
    bool isMatch(string s, string p) {
        vector<InstructionStep> instructions;

        // First build instructions from p
        for (int i = 0; i < p.length(); i++)
        {
            InstructionStep step;
            char c = p[i];
            bool any = c == '.';
            step.val = c;
            if (i < p.length() - 1 && p[i+1] == '*')
            {
                step.type = any ? ANYREPEAT : CHARREPEAT;
                i++;
            }
            else
            {
                step.type = any ? ANY : CHAR;
            }

            instructions.push_back(step);
        }

        return Recurse(s, p, instructions);
    }
};
12181