HardStringDynamic ProgrammingGreedy

Wildcard Matching

LeetCode
1 approach, code in all languages

You are given a text string and a pattern string. The pattern may contain a question mark '?' that matches exactly one arbitrary character and a star '*' that matches any run of characters, including an empty run. Return whether the pattern matches the entire text.

Unlike some other matching rules, here a star stands on its own and can absorb any number of characters, which makes it a flexible gap in the pattern.

Example 1

Input: s = "cat", p = "c?t"

Output: true

The '?' matches the single character 'a', so the pattern lines up with the text.

Example 2

Input: s = "cat", p = "c*b"

Output: false

The star can absorb "at", but the trailing "b" cannot be matched, so the whole pattern fails.

Constraints

  • 0 ≤ s.length ≤ 2 * 10^3
  • 0 ≤ p.length ≤ 2 * 10^3
  • s contains only lowercase English letters
  • p contains lowercase letters, '?', and '*'
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now