HardStringDynamic ProgrammingGreedy

1 approach, code in every language

Problem description

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 '*'

After the pattern

Now solve one you haven't seen.

Dispatch Rule Match is our own problem on the technique behind Wildcard Matching: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free