Skip to content
Onsite

Regular Expression Matching

HardP3Coding

Implement matching of the whole string s against the pattern p, where p may contain:

  • . which matches any single character,
  • * which means zero or more copies of the character (or .) just before it.

Return true only if the pattern matches the entire string, not just part of it. Every * in p follows a letter or a ..

Example 1

Input
s = "aa", p = "a"
Output
false
Why
The pattern is a single `a`, which cannot cover two characters.

Example 2

Input
s = "aa", p = "a*"
Output
true
Why
`a*` matches zero or more `a`s.

Example 3

Input
s = "ab", p = ".*"
Output
true

Constraints

  • 0 <= s.length <= 500
  • 0 <= p.length <= 500
  • s contains lowercase letters; p contains lowercase letters, . and *
  • Every * is preceded by a letter or .

Hints

Run checks the 3 visible tests, Submit all 13.