Regular Expression Matching
Problem
Implement a matcher that decides whether a string s is matched entirely by a pattern p. The pattern may contain lowercase letters, . which matches any single character, and * which means "zero or more repetitions of the element immediately before it". A * is never the first character of p, and the match must cover all of s, not just a prefix.
- Both the text and the pattern are up to 25 characters long.
scontains only lowercase letters;pcontains lowercase letters,., and*.
Examples
s = "bb", p = "b"→false. The pattern consumes oneband stops.s = "bb", p = "b*"→true.b*expands to two b's.s = "cd", p = ".*"→true..*means any character repeated, so it absorbs everything.s = "ccd", p = "x*c*d"→true.x*matches nothing,c*matchescc,dmatchesd.
Hints
- Try to match from the left one unit at a time, where a unit is either a single pattern character or a character followed by
*. - When the next unit is
x*, you have exactly two choices: use it zero times and skip it, or consume one matching character ofsand keep the samex*in play. - Memoise on
(i, j)positions, or fill a bottom-up tabledp[i][j]= doess[i:]matchp[j:].
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.