Regular Expression Matching
1 approach, code in every language
Problem description
You are given a text string and a pattern string. The pattern may contain two special characters: a dot '.' that stands for any single character, and a star '*' that lets the character immediately before it repeat zero or more times. Decide whether the pattern is able to describe the text from beginning to end.
Matching must cover the whole text, not just a prefix. A '*' is never the first symbol of the pattern and always follows an ordinary character or a dot, so every star has something it can repeat.
Example 1
Input: s = "aa", p = "a*"
Output: true
The star lets "a" appear twice, which reproduces "aa" exactly.
Example 2
Input: s = "abc", p = ".*d"
Output: false
".*" can consume "abc", but the trailing "d" then has nothing left to match, so the whole pattern fails.
Constraints
1 ≤ s.length ≤ 201 ≤ p.length ≤ 30s contains only lowercase English lettersp contains lowercase letters, '.', and '*', where every '*' follows a valid character
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.
Mask Matches The Whole Code is our own problem on the technique behind Regular Expression 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