<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 partMatch(string& part, string& sub, int fixed){
        if(fixed){
            return sub == part;
        }
        // not fixed, part contains special characters
        if(part == ".*") return true; // ".*" matches any string
        if(part == ".") return sub.length() == 1;
        // part must be a fixed alphabetic character followed by an '*'
        if(sub.length() == 0) return true; // I think a* matches the empty string
        for(char c : sub){
            if(c != part[0]) return false;
        }
        return true; // all characters in sub are equal to the repeatable character in part, e.g. a in a*
    }
    
    bool regexMatch(string& s, vector<string>& parts, vector<int>& is_fixed, int i, int j){ 
        // i is the idx for parts and is_fixed
        // j is idx in s
        if(i == (int)parts.size() - 1){
            string sub = s.substr(j); // sub can be an empty string, and still successfully match!
            bool match = partMatch(parts[i], sub, is_fixed[i]);
            if(match) return true;
        }
        else{
            if(is_fixed[i] == 1){
                int len = parts[i].length();
                string sub = s.substr(j, len);
                if(partMatch(parts[i], sub, 1)){
                    return regexMatch(s, parts, is_fixed, i + 1, j + len);
                } else return false;
            }
            else{
                for(int len = 0; len <= (int)s.length() - j; ++len){
                    string sub = s.substr(j, len);
                    if(partMatch(parts[i], sub, 0)){
                        bool ret = regexMatch(s, parts, is_fixed, i + 1, j + len);
                        if(ret == true) return true;
                    }
                }
                return false;
            }
        }
        return false;
    }
    
    bool isMatch(string s, string pattern) {
        
        // Leetcode 10. regular expression matching

        // parse out the regular characters, then the special characters such as . by itself, .*, or a*
        // pretty sure there is a condition such that there are no two consecutive **
        // no parentheses in this version
        vector<string> parts; // fixed substrings of the pattern without the special characters
        vector<int> is_fixed;
        string part = "";
        int i = 0;
        while(i < pattern.length()){
            char c = pattern[i];
            if(c == '.'){
                if(part != ""){
                    parts.push_back(part);
                    is_fixed.push_back(1);
                    part = "";
                }

                if(i == (int)pattern.length() - 1){
                    parts.push_back(".");
                    is_fixed.push_back(0);
                    i++;
                }else if(pattern[i + 1] == '*'){
                    parts.push_back(".*");
                    is_fixed.push_back(0);
                    i += 2;
                }else{
                    parts.push_back(".");
                    is_fixed.push_back(0);
                    i++;
                }
            }else{ // alphabetic character. pattern[i] itself guaranteed to never be '*' due to problem constraints, '*' must be pattern[i + 1] the way
                // I increment i
                if(i == (int)pattern.length() - 1){
                    part += c;
                    i++;
                }
                else if(pattern[i + 1] == '*'){
                    if(part != ""){
                        parts.push_back(part);
                        is_fixed.push_back(1);
                        part = "";
                    }
                    parts.push_back(string(1, c) + "*");
                    is_fixed.push_back(0);
                    i += 2;
                }
                else{
                    part += c;
                    i++;
                }
            }
        }
        if(part != ""){
            parts.push_back(part);
            is_fixed.push_back(1);
        }
        return regexMatch(s, parts, is_fixed, 0, 0);
    }
};
11431