Wildcard Matching
Pattern: Strings DP
Match s against a pattern where '?' is any one character and '*' is any run (even empty). ok[i][j] uses the diagonal for a normal character or '?'; for '*', either it matches nothing (left) or it eats one more character (above).
The problem
Return whether the pattern p (with ? for any one character and * for any sequence, including empty) matches all of s.
Think first
- What can '*' do at each step?
- What is ok[0][j]?
- What is ok[i][0] for
i > 0?
Hints
Brute force, and why it fails
Try every way to expand each '*': exponential.
The key insight
Letting '*' take one character at a time keeps every case inside a 2D table.
Dry run
s 'aa', pattern 'a'. The pattern ends after one character.
Step 1 of 2
The plan
ok[0][0] = true; ok[0][j] = ok[0][j-1] and p[j-1] == '*'
if p[j-1] == '*': ok[i][j] = ok[i][j-1] or ok[i-1][j]
else: ok[i][j] = ok[i-1][j-1] and (p[j-1] == '?' or equal)
Complexity
- Time
O(m × n)- Space
O(n)
One row.
Edge cases
- Pattern all '*': true.
- Empty s and pattern '*': true.
- Empty pattern: only an empty s.
I've built my logic, show the code
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[i][j]: s's first i characters match p's first j
dp[0][0] = true;
for (int j = 1; j <= n && p.charAt(j - 1) == '*'; j++) dp[0][j] = true; // leading stars match nothing
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
char c = p.charAt(j - 1);
if (c == '*') dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; // the star matches nothing, or one more character
else dp[i][j] = dp[i - 1][j - 1] && (c == '?' || c == s.charAt(i - 1));
}
return dp[m][n];
}
}C++
class Solution {
public:
bool isMatch(string s, string p) {
int m = static_cast<int>(s.size()), n = static_cast<int>(p.size());
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false)); // s's first i characters match p's first j
dp[0][0] = true;
for (int j = 1; j <= n && p[j - 1] == '*'; j++) dp[0][j] = true; // leading stars match nothing
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
char c = p[j - 1];
if (c == '*') dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; // the star matches nothing, or one more character
else dp[i][j] = dp[i - 1][j - 1] && (c == '?' || c == s[i - 1]);
}
return dp[m][n];
}
};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[i][j]: s's first i characters match p's first j
dp[0][0] = True
for j in range(1, n + 1):
dp[0][j] = dp[0][j - 1] and p[j - 1] == "*" # leading stars match nothing
for i in range(1, m + 1):
for j in range(1, n + 1):
if p[j - 1] == "*":
dp[i][j] = dp[i][j - 1] or dp[i - 1][j] # the star matches nothing, or one more character
else:
dp[i][j] = dp[i - 1][j - 1] and p[j - 1] in ("?", s[i - 1])
return dp[m][n]Common mistakes
- Treating '*' as 'repeat the previous character' (that is regex).
- Missing the leading-star row.