Regular Expression Matching

Implement isMatch

A pattern built from ordinary letters, a single-character wildcard (.), and a repeat marker (*) is checked against a target string for a full match, not a partial one. The tricky part is that * doesn't stand alone — it always modifies whatever came right before it, meaning that character (or .) can appear zero times, once, or many times in a row. A naive check has to be willing to try every repeat count a * pair could possibly use before it knows whether the whole match works out. The way through is the same idea used for matching without repeats: build up an answer to "can this much of the pattern explain this much of the string" one small piece at a time. A * pair offers two independent paths to a yes — skip it completely, or use it once and lean on a smaller version of the same question. Filling in that grid systematically, from the empty prefixes outward, sidesteps the need to ever guess how many repetitions to try.

Example 1:

Input: s = "aa", p = "a"

Output: false

Example 2:

Input: s = "aa", p = "a*"

Output: true

Example 3:

Input: s = "ab", p = ".*"

Output: true

+ 7 hidden test cases run on Submit.

Constraints:

  • 0 ≤ s.length ≤ 10
  • 0 ≤ p.length ≤ 10
  • s consists of lowercase English letters only
  • p consists of lowercase English letters, '.', and '*'
  • It is guaranteed that for each appearance of '*', there is a previous valid character to match

s =

aa

p =

a