Regular Expression Matching

Given an input string s and a pattern p, implement regular expression matching with support for the following special characters:

  • . matches any single character.
  • * matches zero or more of the preceding element.

The matching should cover the entire input string s, not just a substring. Return true if s matches p; otherwise, return false.

Example 1
Inputs = "aa", p = "a"
Outputfalse
The pattern a matches exactly one a, but s contains two characters.
Example 2
Inputs = "aa", p = "a*"
Outputtrue
The * means zero or more of the preceding a, so a* can match aa.

Constraints

  • 1 <= s.length <= 20
  • 1 <= p.length <= 20
  • s contains only lowercase English letters.
  • p contains only lowercase English letters, '.', and '*'.
  • It is guaranteed that for each appearance of '*', there will be a previous valid character to match.

Asked at 27 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate