#44

Wildcard Matching

Hard 2D DP on string and pattern

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether a pattern with '?' (any one character) and '*' (any sequence, including empty) matches the whole string.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: does a pattern with ? and * match the whole string?

Why this works: dp[i][j] for prefixes. '*' matches nothing (left cell) or one more letter (cell above).

Example: s "adceb", p "*a*b"

'*' empty, 'a' matches, '*' eats "dce", 'b' matches → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the leading-'*' row, so patterns like "*a" fail to match.

Solve on LeetCode ↗
Complexity
  • Time: O(n·m).
  • Space: O(n·m).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon