HardStringDynamic ProgrammingRecursion

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 ≤ 20
  • 1 ≤ p.length ≤ 30
  • s contains only lowercase English letters
  • p contains lowercase letters, '.', and '*', where every '*' follows a valid character

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