Wildcard Matching - Problem

Given an input string s and a pattern p, implement wildcard pattern matching with support for '?' and '*':

  • '?' Matches any single character
  • '*' Matches any sequence of characters (including the empty sequence)

The matching should cover the entire input string (not partial). Return true if the pattern matches the string, false otherwise.

Input & Output

Example 1 — Basic Wildcard Match
$ Input: s = "abc", p = "a*c"
Output: true
💡 Note: Pattern 'a*c' matches 'abc': 'a' matches 'a', '*' matches 'b', 'c' matches 'c'
Example 2 — No Match
$ Input: s = "abc", p = "a*d"
Output: false
💡 Note: Pattern 'a*d' cannot match 'abc': 'a' matches 'a', '*' can match 'b', but 'd' cannot match 'c'
Example 3 — Question Mark Wildcard
$ Input: s = "abc", p = "a?c"
Output: true
💡 Note: Pattern 'a?c' matches 'abc': 'a' matches 'a', '?' matches 'b', 'c' matches 'c'

Constraints

  • 0 ≤ s.length, p.length ≤ 2000
  • s contains only lowercase English letters
  • p contains only lowercase English letters, '?' or '*'

Visualization

Tap to expand
Wildcard Matching: How Patterns WorkStringabcPatterna*cexactany seqexactWildcard Rules:• '?' matches any single character• '*' matches 0 or more characters• Must match entire string✅ Result: true - Pattern matches entire string
Understanding the Visualization
1
Input
String 'abc' and pattern 'a*c' with wildcards
2
Process
Match character by character, handling wildcards
3
Output
True if entire string matches pattern
Key Takeaway
🎯 Key Insight: Use DP to break the problem into subproblems: does pattern[0:j] match string[0:i]?
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
78.0K Views
Medium Frequency
~25 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen