Home
DSA Patterns
Dynamic Programming
Regular Expression Matching
Regular Expression Matching (LeetCode 10)
Regular Expression Matching (LeetCode 10): match a string against a pattern with '.' and '*' using a 2D DP table: a star pair matches zero copies or eats one more letter.
Problem Understanding
Regular Expression Matching: implement matching of a string s against a pattern p where . matches any single letter and * matches zero or more of the element right before it. The match must cover the whole string.
Examples: s = "aa", p = "a" is false; p = "a*" is true; s = "ab", p = ".*" is true.
The Intuition: Treat 'x*' as One Unit
Read the pattern in units: a single letter, a ., or a pair x*. Now ask whether the first i letters of s match the first j characters of p:
- A letter or
.must matchs[i - 1], and the rest must already match:dp[i - 1][j - 1]. - A pair
x*used zero times disappears:dp[i][j - 2]. - A pair
x*used once more eatss[i - 1]ifxmatches it, and the same pair stays available:dp[i - 1][j].
The third case is the key: staying in column j lets one pair absorb any number of letters, one row at a time. Without a table, trying every count for every star repeats the same subproblems again and again; the (m + 1) × (n + 1) grid computes each one once. It is the same two-string table as Edit Distance and Interleaving String, a core dynamic programming pattern.
Regular Expression Matching Visualization
EMPTY MATCHES EMPTY
∅
c
*
a
*
b
∅
a
a
b
cell
:
match
:
dp[0][0] = true: the empty pattern matches the empty string. Column 0 below it stays false
Next
'c' needs a letter, and the string is empty: dp[0][1] = false
s
p
The pattern runs across the top and the string down the left. Each cell shows T or F, and the cell its true came from is tinted. Try Trailing c to watch .* fill a whole column with T while the final c stays F.
Regular Expression Matching Dry Run
Input: s = "aab" (down), p = "c*a*b" (across)
| ∅ | c | * | a | * | b | |
|---|---|---|---|---|---|---|
| ∅ | T | F | T | F | T | F |
| a | F | F | F | T | T | F |
| a | F | F | F | F | T | F |
| b | F | F | F | F | F | T |
- Row ∅:
c*andc*a*match the empty string (zero copies), so those columns are T. - Row 2, column
a*:a*eats the seconda, reading the T above it. - Row 3, column
b:bmatches and the diagonaldp[2][4]is T.
Answer: true.
Regular Expression Matching LeetCode Solution
A bottom-up table that runs row 0 too, so star pairs can match the empty string. LeetCode guarantees every * follows a valid character, so j - 2 is never negative there.
Regular Expression Matching Code
1var isMatch = function(s, p) {2 const m = s.length, n = p.length;3 const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(false));4 dp[0][0] = true;5 const matches = (i, j) => p[j] === '.' || p[j] === s[i];6 for (let i = 0; i <= m; i++) {7 for (let j = 1; j <= n; j++) {8 if (p[j - 1] === '*') {9 dp[i][j] = dp[i][j - 2]10 || (i > 0 && matches(i - 1, j - 2) && dp[i - 1][j]);11 } else {12 dp[i][j] = i > 0 && matches(i - 1, j - 1) && dp[i - 1][j - 1];13 }14 }15 }16 return dp[m][n];17};
Regular Expression Matching in Python, Java, C++, TypeScript and C
Regular Expression Matching in Python
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
for i in range(m + 1):
for j in range(1, n + 1):
if p[j - 1] == '*':
dp[i][j] = dp[i][j - 2] or (
i > 0 and p[j - 2] in (s[i - 1], '.') and dp[i - 1][j])
else:
dp[i][j] = i > 0 and p[j - 1] in (s[i - 1], '.') and dp[i - 1][j - 1]
return dp[m][n]Regular Expression Matching in Java
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length(), n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int i = 0; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 2]
|| (i > 0 && matches(s, p, i - 1, j - 2) && dp[i - 1][j]);
} else {
dp[i][j] = i > 0 && matches(s, p, i - 1, j - 1) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
private boolean matches(String s, String p, int i, int j) {
return p.charAt(j) == '.' || p.charAt(j) == s.charAt(i);
}
}Regular Expression Matching in C++
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
dp[0][0] = true;
for (int i = 0; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i][j - 2]
|| (i > 0 && (p[j - 2] == '.' || p[j - 2] == s[i - 1]) && dp[i - 1][j]);
} else {
dp[i][j] = i > 0 && (p[j - 1] == '.' || p[j - 1] == s[i - 1]) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};Regular Expression Matching in TypeScript
function isMatch(s: string, p: string): boolean {
const m = s.length, n = p.length;
const dp: boolean[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(false));
dp[0][0] = true;
const matches = (i: number, j: number): boolean => p[j] === '.' || p[j] === s[i];
for (let i = 0; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (p[j - 1] === '*') {
dp[i][j] = dp[i][j - 2]
|| (i > 0 && matches(i - 1, j - 2) && dp[i - 1][j]);
} else {
dp[i][j] = i > 0 && matches(i - 1, j - 1) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}Regular Expression Matching in C
bool isMatch(char* s, char* p) {
int m = strlen(s), n = strlen(p);
bool dp[m + 1][n + 1];
memset(dp, 0, sizeof(dp));
dp[0][0] = true;
for (int i = 0; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i][j - 2]
|| (i > 0 && (p[j - 2] == '.' || p[j - 2] == s[i - 1]) && dp[i - 1][j]);
} else {
dp[i][j] = i > 0 && (p[j - 1] == '.' || p[j - 1] == s[i - 1]) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}Edge Cases & Common Mistakes
- Skipping row 0: patterns like
"a*b*"match the empty string; without row 0,s = "b",p = "a*b"returns false. - Treating
*as a wildcard on its own: it always modifies the character before it;"a*"can be empty,"a"cannot. A standalone*is Wildcard Matching. - Partial matches: the whole string must match, so
"a"does not match"aa". .*is not "anything at the end":".*c"still needs a finalc.- Recursion without memo: each star branches on zero or one more copy; without caching
(i, j)the work grows exponentially. Memoised top-down recursion gives the same O(m · n) as the table.
Summary
Set dp[0][0] = true and fill every row, including the empty string. A letter or . reads the diagonal; x* reads two to the left (zero copies) or straight up when x matches (one more copy). Return dp[m][n]. O(m · n) time and space.
More practice: DSA interview questions with answers · patterns cheat sheet
