Wildcard Matching
Problem Statement
You get a string s and a pattern p. Decide whether the pattern matches the whole string.
The pattern can use two special characters. ? matches any one letter. * matches any sequence of letters, including the empty sequence. Every other pattern character must match the same letter in s.
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.
* matches any sequence, so here it matches both letters.
? matches c, but the second pattern character is a and the string has b there.
- • 0 ≤ s.length, p.length ≤ 2000
- • s contains only lowercase English letters.
- • p contains only lowercase English letters, `?` or `*`.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse on (i, j): the last pattern character decides the move
dp[i][j] = does the first j pattern characters match the first i characters of s, filled once per cell
Keep one row over the pattern; a star reads the cell to its left and the cell above
Let match(i, j) say whether the first j pattern characters match the first i characters of s. Look at the last pattern character. A letter or ? must match s[i − 1], and then we continue with match(i − 1, j − 1). A * either matches nothing, which is match(i, j − 1), or takes s[i − 1] and stays on the star, which is match(i − 1, j). The empty pattern matches only the empty string.
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.