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 '*'See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
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