HardStringDynamic ProgrammingRecursion

Regular Expression Matching

LeetCode
1 approach, code in all languages

You are given a text string and a pattern string. The pattern may contain two special characters: a dot '.' that stands for any single character, and a star '*' that lets the character immediately before it repeat zero or more times. Decide whether the pattern is able to describe the text from beginning to end.

Matching must cover the whole text, not just a prefix. A '*' is never the first symbol of the pattern and always follows an ordinary character or a dot, so every star has something it can repeat.

Example 1

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

Output: true

The star lets "a" appear twice, which reproduces "aa" exactly.

Example 2

Input: s = "abc", p = ".*d"

Output: false

".*" can consume "abc", but the trailing "d" then has nothing left to match, so the whole pattern fails.

Constraints

  • 1 ≤ s.length ≤ 20
  • 1 ≤ p.length ≤ 30
  • s contains only lowercase English letters
  • p contains lowercase letters, '.', and '*', where every '*' follows a valid character
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now