Skip to main content
Back to problems
Leetcode
Medium
Strings
Dynamic Programming
Recursion
Google
Amazon
Wildcard Matching

Determine whether a pattern containing ? and * can match an entire input string.

Acceptance 100%
Problem Statement

Problem

You are given two strings:

  • s: the input text
  • p: a pattern that may contain ordinary lowercase characters plus two wildcard symbols:
    • ? matches exactly one character
    • * matches any sequence of characters, including the empty sequence

Return whether the entire string s can be matched by the entire pattern p.

A match must consume all characters in both strings. Partial matches do not count.

Input Format

  • A string s
  • A string p

Both strings are composed of lowercase letters and the pattern may additionally contain ? and *.

Output Format

  • Return true if p matches all of s
  • Otherwise return false

Constraints

  • Match must cover the full string and full pattern
  • ? matches exactly one character
  • * matches zero or more characters
  • Strings are assumed to be non-empty or empty depending on the test case
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "adceb", p = "*a*b"

Output

true

Explanation

* can match the empty prefix, then a matches a, the second * matches dce, and b matches b.

Example 2

Input

s = "acdcb", p = "a*c?b"

Output

false

Explanation

No assignment of characters to the wildcards allows the pattern to consume the entire string while preserving the fixed letters.

Show 1 more example

Example 3

Input

s = "", p = "*"

Output

true

Explanation

* can match the empty sequence.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.