<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>

#include <queue>
class Solution {
public:
    bool CorrectCh(int idx_s, int idx_p) {
        return s_[idx_s] == p_[idx_p].first || p_[idx_p].first == '.';
    } 
    void MakeStep(int idx_s, int idx_p) {
        if (idx_p + 1 < p_.size() && p_[idx_p+1].second) {
            q_.push(std::make_pair(idx_s, idx_p + 1));
        }
        if (idx_s + 1 < s_.length() && idx_p >= 0) {
            if (p_[idx_p].second && CorrectCh(idx_s + 1, idx_p)) {
                q_.push({idx_s + 1, idx_p});
            }
        }
        if (idx_s + 1 < s_.length() && idx_p + 1 < p_.size()) {
            if (CorrectCh(idx_s + 1, idx_p + 1)) {
                q_.push({idx_s + 1, idx_p + 1});
            }
        }
    }
    bool isMatch(string s, string p) {
        s_ = s;
        p_ = ProcessP(p);
        q_.push({-1, -1});
        while(!q_.empty()) {
            auto pr = q_.front();
            q_.pop();
            if (pr.first == s_.length() -1 && pr.second == p_.size() - 1) {
                return true;
            }
            MakeStep(pr.first, pr.second); 
        }
        return false;
    }
    std::vector<std::pair<char, bool>> ProcessP(const std::string& p) {
        std::vector<std::pair<char, bool>> output;
        for (int i = 0; i < p.length(); ++i) {
            bool next_star = i + 1 < p.length() && p[i+1] == '*'; 
            if (next_star) {
                if (output.empty() || output.rbegin()->second == false || output.rbegin()->first != p[i]) {
                    output.push_back(std::make_pair(p[i], true));
                }
                ++i;
            } else {
                output.push_back(std::make_pair(p[i], false));
            }
        }
        return output;
    }
    std::string s_;
    std::vector<std::pair<char, bool>> p_;
    std::queue<std::pair<int, int>> q_;
};
17806