<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:
    def isMatch(self, S: str, P: str) -> bool:
        
        
        
        
        
        
        # public solution ... 92 ms ... 92 % ... 13.3 MB ... 67 %
        # fucking genius
        
        # ls, lp = len(S), len(P)
        # si, pi = 0, 0                                   # we are matching S[si:] with P[pi:]
        # ss, pp = -1, -1                                 # pp is previous '*' index in P, while ss is previous '*' matched index in S
        # while si < ls:
        #     if pi < lp and P[pi] == '*':
        #         ss, pp = si, pi
        #         pi += 1                                 # in initial state, we begin from '*' matches empty string ''
        #     elif (pi < lp and P[pi] == '?') or (pi < lp and P[pi] == S[si]):
        #         si, pi = si+1, pi+1
        #     else:                                       # NOW, under previous match assume for '*', error occurred, we need restart from '*'
        #         if pp == -1:
        #             return False
        #         else:
        #             si, pi = ss+1, pp+1                 # increase previous '*' match length by 1
        #             ss += 1                             # increase ss by 1, for convenience of furture revisit
        # return P[pi:].count('*') == lp-pi
        
        
        
        
        
        
        # my solution ... 2473 ms ... 
        
        seen = {}
        def match(i, j):    # 返回 s[i:] 和 p[j:] 的匹配结果
            if i == len(s):
                return p[j:].count('*') == (len(p) - j)    # p[j:]必须全是 *，不能有其他字符
            if j == len(p):
                return False
            if (i, j) not in seen:
                if p[j] == '?':
                    res = match(i+1, j+1)
                elif p[j] == '*':
                    res = any(match(ni, j+1) for ni in range(i, len(s)+1))
                else:
                    res = (s[i] == p[j]) and match(i+1, j+1)
                seen[i, j] = res
            return seen[i, j]
        
        s, p = S, P
        return match(0, 0)
        
        
        
        
        
        
81936