36 · DP Patterns

#10 Regular Expression Matching

Hardsolve on LeetCode ↗

frequently reported atAirbnbByteDance / TikTokAmazonGoogleMetaMicrosoft

the problem

In plain words

Match the whole string s against a pattern p built from lowercase letters, '.' and '*'. A '.' stands for any one character. A star after a character x means zero or more copies of x (x may be '.'). Return true only if the pattern covers all of s, not just a part of it.

in s = "aa", p = "a"

out false

"a" covers only one of the two letters.

in s = "aa", p = "a*"

out true

a* repeats a twice.

in s = "ab", p = ".*"

out true

.* is any number of any characters.

  • 1 ≤ s.length ≤ 20
  • 1 ≤ p.length ≤ 20
  • s has lowercase letters; p has lowercase letters, '.' and '*'
  • Every '*' comes right after a letter or a '.'

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #72
      Edit Distance
      Mediumin Dynamic Programming
    2. #97
      Interleaving String
      Mediumin DP Patterns
    3. #139
      Word Break
      Mediumin Dynamic Programming