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

import numpy as np

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        n_s = len(s)
        n_p = len(p)

        if (s == p) or (set(p) == set("*")):
            return True
        if (s == "") or (p == ""):
            return False
        
        match_grid = np.zeros((n_p + 1, n_s + 1), dtype=bool)
        match_grid[0, 0] = True

        for i_p in range(n_p):
            # NOTE: for each element in the pattern, we will process the whole string s
            if p[i_p] == "*":
                i_s = 0
                while (not match_grid[i_p][i_s]) and (i_s < n_s):
                    i_s += 1
                
                # NOTE: i_p is the index of the pattern p,
                # but its corresponding entry in the DP grid is i_p + 1 for the first (i_p + 1) elements
                match_grid[i_p + 1][i_s] = match_grid[i_p][i_s]

                while i_s < n_s:
                    # NOTE: if we can execute this branch, then in the above while loop,
                    # we must have somewhere match_grid[i_p][i_s] == True
                    match_grid[i_p + 1][i_s + 1] = True
                    i_s += 1
            elif p[i_p] == "?":
                for i_s in range(n_s):
                    match_grid[i_p + 1][i_s + 1] = match_grid[i_p][i_s]
            else:
                for i_s in range(n_s):
                    match_grid[i_p + 1][i_s + 1] = (p[i_p] == s[i_s]) and match_grid[i_p][i_s]
        
        return match_grid[n_p][n_s]
    
    def recur_isMatch(self, s: str, p: str) -> bool:

        def _remove_dup(p: str):
            new = []
            for tmp in p:
                if (not new) or (tmp != "*"):
                    new.append(tmp)
                elif new[-1] != "*":
                    new.append(tmp)
            return "".join(new)
        
        dp = {}

        def _help(s: str, p: str):
            if (s, p) in dp:
                return dp[(s, p)]

            if (p == s) or (set(p) == set("*")):
                dp[(s, p)] = True
            elif (p == "") or (s == ""):
                dp[(s, p)] = False
            elif (p[0] == s[0]) or (p[0] == "?"):
                dp[(s, p)] = _help(s[1:], p[1:])
            elif p[0] == "*":
                dp[(s, p)] = _help(s, p[1:]) or _help(s[1:], p)
            else:
                dp[(s, p)] = False

            return dp[(s, p)]

        p = _remove_dup(p)
        return _help(s, p) 

29628