<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 preprocess_pattern(self, p: str) -> str:
        while '*' in p:
            mid_idx = p.find('*')
            for start_idx in range(mid_idx, -1, -1):
                if p[start_idx] not in ['*', '?']:
                    start_idx += 1
                    break
            for end_idx in range(mid_idx, len(p)):
                if p[end_idx] not in ['*', '?']:
                    break
            if p[end_idx] in ['*', '?']:
                end_idx += 1
            problem_subpattern = p[start_idx:end_idx]
            num_qs = problem_subpattern.count('?')
            p = p[:start_idx] + '?' * num_qs + '$' + p[end_idx:]
        p = p.replace('$', '*')
        return p
    
    def isMatch(self, s: str, p: str) -> bool:
        p = self.preprocess_pattern(p)
        return self.isMatchRecur(s, p)

    def isMatchRecur(self, s: str, p: str) -> bool:
        if len(p) == 0:
            if len(s) != 0:
                return False
            else:
                return True
        c = p[0]
        if len(s) == 0:
            if c == '*' and len(p) == 1:
                return True
            else:
                return False
        if c == '?':
            return self.isMatch(s[1:], p[1:])
        elif c == '*':
            if '*' not in p[1:]:
                if len(p) == 1:
                    return True
                for i in range(len(s) + 1):
                    if self.isMatch(s[i:], p[1:]):
                        return True
                return False
            p_end_idx = 1 + p[1:].find('*')
            easier_p = p[1:p_end_idx + 1]
            found_idx = False
            for idx in range(len(s) + 1):
                if self.isMatch(s[idx:], easier_p):
                    found_idx = True
                    break
            if not found_idx:
                return False
            return self.isMatch(s[idx + len(easier_p) - 1:], p[p_end_idx:])
        else:
            if c == s[0]:
                return self.isMatch(s[1:], p[1:])
            else:
                return False
        
        
18805