Skip to content

Regular Expression Matching

HardDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Implement a tiny regular expression matcher. The pattern p contains lowercase letters plus two special characters:

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

Return true if p matches the entire string s (not just part of it).

s = "aa",  p = "a"      →  false   (only one a)
s = "aa",  p = "a*"     →  true
s = "ab",  p = ".*"     →  true    (any characters, any number of times)
s = "aab", p = "c*a*b"  →  true    (zero c's, two a's, one b)

Constraints

  • 1 <= s.length <= 20
  • 1 <= p.length <= 20
  • s contains only lowercase letters; p contains lowercase letters, . and *.
  • Every * comes right after a letter or ..