<p>Given an input string (<code>s</code>) and a pattern (<code>p</code>), implement wildcard pattern 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 any sequence of characters (including the empty sequence).</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;*&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong>&nbsp;&#39;*&#39; matches any sequence.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cb&quot;, p = &quot;?a&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong>&nbsp;&#39;?&#39; matches &#39;c&#39;, but the second letter is &#39;a&#39;, which does not match &#39;b&#39;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length, p.length &lt;= 2000</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> or <code>&#39;*&#39;</code>.</li>
</ul>

class Solution {
public:
    bool isMatch(string s, string p) {
        vector<string> tokens{};
        string token = "";
        for(char c: p) {
            if(c == '*') {
                if(tokens.size() > 0 && tokens.back() == "*" && token.size() == 0) {
                    // consecutive wild cards are redundant
                    continue;
                }
                if(token.size() > 0) {
                    // if we have a token to match
                    tokens.push_back(token);
                    token = "";
                }
                // register the wild card
                tokens.push_back("*"); 
            } else {
                token +=c;
            }
        } 
        if(token.size() > 0) {
            tokens.push_back(token);
        }
        // check empty string
        if(s == "" && tokens.size() == 1 && tokens[0] == "*") {
            return true;
        }
        // current token being processed
        int token_index = 0;
        for(int i = 0; i < s.size(); i++) {
            if(token_index >= tokens.size()) {
                return false;
            }
            if(tokens[token_index] == "*") {
                if(token_index == tokens.size() - 1) {
                    return true;
                } 
                string peek = tokens[token_index + 1];
                if(s.size() - i < peek.size()) {
                    return false;
                }
                if(pattern_match(s.substr(i, peek.size()), peek)) {
                    if(token_index == tokens.size() - 2 && i + peek.size() < s.size()) {
                        continue;
                    }
                    token_index += 2;
                    i += peek.size() - 1;
                }
            } else {
                string t = tokens[token_index];
                if(s.size() - i < t.size()) {
                    return false;
                }
                if(pattern_match(s.substr(i, t.size()), t)) {
                    token_index += 1;
                    i += t.size() - 1;
                } else {
                    return false;
                }
            }
        }
        if(token_index == tokens.size() - 1 && tokens[token_index] == "*") {
            return true;
        }
        return token_index == tokens.size();
    }
    bool pattern_match(string text, string pattern) {
        if(text.size() != pattern.size()) {
            return false;
        }
        for(int i = 0; i < pattern.size(); i++) {
            if(pattern[i] == '?') {
                continue;
            }
            if(pattern[i] != text[i]) {
                return false;
            }
        }
        return true;
    }
};
12306