Wildcard Matching

Implement isMatch

A pattern built from ordinary letters, single-character wildcards (?), and multi-character wildcards (*) is checked against a target string — the question is whether the pattern can be stretched or squeezed to cover the string entirely, not just a piece of it. Letters and ? are easy: they simply have to line up one-to-one. The * is what makes this genuinely tricky, since it isn't pinned to any particular number of characters, so a naive check has to be willing to try every length it could possibly absorb. The way through is to stop asking "how many characters does this * eat" up front, and instead ask a simpler yes/no question at every (position in s, position in p) pair: can the pattern up to here match the string up to here? A * then has just two ways to answer yes — either it isn't needed at all here (check without it), or it's already covering the last character of s and the same question just shifts one character earlier in s. Chaining that logic across the whole grid answers the original question without ever guessing a length.

Example 1:

Input: s = "aa", p = "a"

Output: false

Example 2:

Input: s = "aa", p = "*"

Output: true

Example 3:

Input: s = "", p = "?"

Output: false

+ 7 hidden test cases run on Submit.

Constraints:

  • 0 ≤ s.length ≤ 10
  • 0 ≤ p.length ≤ 10
  • s consists of lowercase English letters only
  • p consists of lowercase English letters, '?', and '*'

s =

aa

p =

a