Algo-Sim › Advanced DP
Regular Expression Matching
Hard · Advanced DP · step through it in Java, Python and C++ on your own input
Match s against a pattern with '.' and '*'. A 2D table of prefix pairs: a star either vanishes or eats one more letter.
Problem
Implement matching of a string s against a pattern p where . matches any single character and x* matches zero or more copies of the character x before it. The pattern must match the whole string, not just a part of it.
Constraints
- 1 ≤ s.length, p.length ≤ 20
- s contains lowercase letters; p contains lowercase letters,
.and* - Every
*follows a valid character
Edge cases to test
- Pattern
"a*"against"":true ".*"matches anything- A star after a dot inside a longer pattern:
"a.*b"
Follow-up questions
- What is the cost?
- O(m·n) time and space for string length m and pattern length n; one row of the table is enough for space O(n).
- How does a real regex engine do it?
- Compile the pattern to an NFA and simulate the set of states (Thompson’s construction): O(m·n) with no backtracking blow-up.
Solution
Java
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 j = 2; j <= n; j++)
dp[0][j] = p.charAt(j - 1) == '*' && dp[0][j - 2];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
char pc = p.charAt(j - 1);
if (pc == '*') {
boolean zero = dp[i][j - 2];
boolean more = matches(s.charAt(i - 1), p.charAt(j - 2)) && dp[i - 1][j];
dp[i][j] = zero || more;
} else {
dp[i][j] = matches(s.charAt(i - 1), pc) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
private boolean matches(char sc, char pc) {
return pc == '.' || pc == sc;
}Python
def is_match(s, p):
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
for j in range(2, n + 1):
dp[0][j] = p[j - 1] == '*' and dp[0][j - 2]
def matches(sc, pc):
return pc == '.' or pc == sc
for i in range(1, m + 1):
for j in range(1, n + 1):
if p[j - 1] == '*':
zero = dp[i][j - 2]
more = matches(s[i - 1], p[j - 2]) and dp[i - 1][j]
dp[i][j] = zero or more
else:
dp[i][j] = matches(s[i - 1], p[j - 1]) and dp[i - 1][j - 1]
return dp[m][n]C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isMatch(string &s, string &p) {
int m = int(s.size()), n = int(p.size());
vector<vector<bool>> dp = vector<vector<bool>>(m + 1, vector<bool>(n + 1));
dp[0][0] = true;
for (int j = 2; j <= n; j++)
dp[0][j] = p[j - 1] == '*' && dp[0][j - 2];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
char pc = p[j - 1];
if (pc == '*') {
bool zero = dp[i][j - 2];
bool more = matches(s[i - 1], p[j - 2]) && dp[i - 1][j];
dp[i][j] = zero || more;
} else {
dp[i][j] = matches(s[i - 1], pc) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
bool matches(char sc, char pc) {
return pc == '.' || pc == sc;
}
};
More Advanced DP problems
- Burst Balloons (Hard)
- Travelling Salesman (Held-Karp bitmask DP) (Hard)
- Partition to K Equal Sum Subsets (Medium)
- House Robber III (Medium)
- Longest Increasing Path in a Matrix (Hard)
- Wildcard Matching (Hard)
- Best Time to Buy and Sell Stock IV (Hard)