Wildcard Matching
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^30 ≤ p.length ≤ 2 * 10^3s contains only lowercase English lettersp contains lowercase letters, '?', and '*'
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
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