<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 {
    typedef pair<int, char> Key;
    typedef set<int> Val;
    map<Key, Val> table;
    int accept;
public:
    // s = [a-z]* ; p = [a-z.*]* & valid (for * there is always a preceding character [a-z.])
    bool isMatch(string s, string p) {
        string q;
        const char *ptr = p.c_str();
        char chr;
        while (*ptr) {
            chr = *ptr++;
            if (chr >= 'a' && chr <= 'z' || chr == '.') {
                if (*ptr == '*') {
                    ptr++;
                    q.push_back(chr - 32);
                }
                else {
                    q.push_back(chr);
                }
            }
            else {
                cout << "Invalid character " << chr << endl;
                return false;
            }
        }
        cout << "Modified pattern = " << q << endl;
        ptr = q.c_str();
        char last = *ptr;
        string r{ initializer_list<char>{ *ptr++ } };
        while (*ptr) {
            chr = *ptr++;
            if ((chr >= 'A' && chr <= 'Z' || chr == 14) && chr == last) {
                continue;
            }
            r.push_back(chr);
            last = chr;
        }
        cout << "Shortened pattern = " << r << endl;
        ptr = r.c_str();
        int cur = 0;
        while (*ptr) {
            chr = *ptr++;
            if (!(chr >= 'A' && chr <= 'Z' || chr >= 'a' && chr <= 'z' || chr == '.' || chr == 14)) {
                cout << "Invalid character " << chr << endl;
                return false;
            }
            if (chr >= 'A' && chr <= 'Z' || chr == 14) {
                update_set(Key{ cur, '\0' }, cur + 1);
                update_set(Key{ cur + 1, chr + 32 }, cur + 1);
            }
            else {
                update_set(Key{ cur, chr }, cur + 1);
            }
            cur++;
        }
        accept = cur;

        for (const auto &[index, states] : table) {
//            if (index.second == '.') { continue; }
            cout << "[" << index.first << ";" << index.second << "] -> ";
            const char *sep = "";
            for (const auto &state : states) {
                cout << sep << state;
                sep = " | ";
            }
            cout << endl;
        }

        for (const auto &[index, states] : table) {
            if (index.second == '.') {
                for (const auto &chr : string{ "abcdefghijklmnopqrstuvwxyz" } ) {
                    for (const auto &state : states) {
                        update_set(Key{ index.first, chr }, state);
                    }
                }
            }
        }


        return match(s.c_str(), 0);
    }

    bool match(const char *input, int state) {
        cout << "Trying " << state << " on '" << input << "'" << endl;
        // empty transitions
        auto found_empty = table.find(make_pair(state, '\0'));
        if (found_empty != table.cend()) {
            for (const auto &next : found_empty->second) {
                if (match(input, next)) {
                    return true;
                }
            }
        }
        if (*input == '\0') {
            return state == accept;
        }
        auto found = table.find(make_pair(state, *input));
        if (found == table.cend()) {
            return false;
        }
        input++;
        for (const auto &next : found->second) {
            if (match(input, next)) {
                return true;
            }
        }
        return false;
    }

    void update_set(const Key &idx, int state) {
        auto found = table.find(idx);
        if (found == table.cend()) {
            table[idx] = Val{ initializer_list<int>{ state } };
        }
        else {
            found->second.insert(state);
        }
    }
};

15368