Skip to content
BASA DSA

Regular Expression Matching

Hard2-D Dynamic Programming

Return whether the entire string s matches pattern p. A dot matches one character; a star repeats the preceding literal or dot zero or more times. No other regex features exist.

Example 1

Input
s = "aab"
p = "c*a*b"
Output
true

Explanation

c uses zero c letters, a uses two a letters, and b consumes the final b.

Example 2

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

Explanation

.* consumes both a and b because dot matches either.

Example 3

Input
s = "aaa"
p = "a"
Output
false

Explanation

The literal a cannot consume all three characters; partial matching is insufficient.

Example 4

Input
s = ""
p = "a*"
Output
true

Explanation

a* repeats zero times to match the empty string.

Constraints

  • 0 <= len(s),len(p) <= 20
  • s is lowercase
  • p contains lowercase letters, dots and stars
  • Each star has a valid preceding atom

Function contractisMatchArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…