Regular Expression Matching
Problem Statement
You get a string s and a pattern p. Decide whether the pattern matches the whole string.
The pattern uses two special characters. . matches any one letter. x* means zero or more copies of x, where x is the letter or . just before the star. A star belongs only to the character right before it.
A string s and a pattern p.
true if p matches all of s, otherwise false.
a matches only one letter, and s has two.
a* means zero or more a's. Here it takes both letters.
. matches any letter, and the star repeats it. So .* matches any string.
- • 1 ≤ s.length ≤ 20
- • 1 ≤ p.length ≤ 20
- • s contains only lowercase English letters.
- • p contains only lowercase English letters, `.` and `*`.
- • Every `*` has a valid character before it to repeat.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse on (i, j): a starred pair takes zero copies, or one more copy of its letter
dp[i][j] = does the first j pattern characters match the first i letters of s, each cell once
Keep one row over the pattern; a star reads the new cell two back and the old cell above
Let match(i, j) say whether the first j pattern characters match the first i characters of s. If the last pattern character is a star, the pair x* before it is one choice. Zero copies skips both characters: match(i, j − 2). One more copy needs x to match s[i − 1], and then the star stays for more copies: match(i − 1, j). Any other character matches one letter, as usual.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.