Wildcard Matching
Hard
Topics
Given an input string s and a pattern p, return true if p matches the entire s. The pattern supports '?' (matches any single character) and '*' (matches any sequence of characters, including the empty sequence).
Example 1
? stands for exactly one character and * for any run, including none at all — which is the case that catches people out. Here the pattern cannot cover it, so false.Input: s = "aa", p = "a" Output: false
Example 2
Input: s = "aa", p = "*" Output: true
Example 3
Input: s = "cb", p = "?a" Output: false
Constraints
- 0 <= s.length, p.length <= 16
- s contains only lowercase letters.
- p contains lowercase letters, '?' and '*'.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Two prefixes determine the state: how much of s and how much of the pattern have been consumed. dp[i][j] is whether the first i characters of s match the first j of the pattern.
The non-star cases are forced. A literal or ? consumes exactly one character from each side, so dp[i][j] = dp[i-1][j-1] when they are compatible. A * has two options and the recurrence takes both: dp[i-1][j] means the star absorbs s[i-1] and stays available, and dp[i][j-1] means the star matches nothing and is done.
The first row is the part that is easy to forget. An empty s matches a pattern of stars only, which is why dp[0][j] = dp[0][j-1] propagates along leading stars and stops at the first non-star.
Approach
- Set
dp[0][0] = true. - Fill the first row:
dp[0][j] = dp[0][j-1]when the pattern character is a star. - For each
i, j: on a star takedp[i-1][j] or dp[i][j-1]; on?or a literal match takedp[i-1][j-1]; otherwise false. - Return
dp[n][m].
Dry Run
s = "adceb" p = "*a*b"
'' * a * b
'' T T . . .
a . T T T .
d . T . T .
c . T . T .
e . T . T .
b . T . T T
answer true the first * matches "", then 'a', then * matches "dce", then 'b'
"" vs "*" true a star can match nothing
"" vs "" true
"a" vs "" false
"aa" vs "a" false
"acdcb" vs "a*c?b" falseComplexity
- Time — O(|s| · |p|): one constant-time cell each.
- Space — O(|s| · |p|) for the table, reducible to O(|p|) by keeping two rows.
- There is also a greedy two-pointer solution that runs in O(|s| + |p|) time and O(1) space: walk both strings, and on a star remember its position and the input position so a later mismatch can backtrack to "let the star eat one more character". It is the better answer and markedly easier to get wrong, which is why the table is the one to write first.
Common Pitfalls
- Forgetting the first-row initialisation, which makes every pattern with a leading star fail against a short input.
- Writing the star case as
dp[i-1][j-1] or dp[i][j-1]. The star must be able to absorb a character and remain available, which isdp[i-1][j]. - Treating
*as matching exactly one character, or as needing at least one. It matches the empty sequence too. - Confusing this with regular-expression matching, where
*applies to the preceding character. Here it is a standalone wildcard. - Returning early on a literal mismatch. A star earlier in the pattern may still rescue it, which is precisely what the table explores.
- In the greedy variant, failing to reset the pattern pointer to just after the remembered star on a mismatch, or forgetting to advance the remembered input position — which makes it loop forever.
Full Solution — all languages
class Solution:
def isMatch(self, s, t):
n, m = len(s), len(t)
dp = [[False] * (m + 1) for _ in range(n + 1)]
dp[0][0] = True
for j in range(1, m + 1):
if t[j - 1] == '*':
dp[0][j] = dp[0][j - 1]
for i in range(1, n + 1):
for j in range(1, m + 1):
if t[j - 1] == '*':
dp[i][j] = dp[i - 1][j] or dp[i][j - 1]
elif t[j - 1] == '?' or t[j - 1] == s[i - 1]:
dp[i][j] = dp[i - 1][j - 1]
return dp[n][m]import java.util.*;
class Solution {
public boolean isMatch(String s, String t) {
int n = s.length(), m = t.length();
boolean[][] dp = new boolean[n + 1][m + 1];
dp[0][0] = true;
for (int j = 1; j <= m; j++) if (t.charAt(j - 1) == '*') dp[0][j] = dp[0][j - 1];
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
if (t.charAt(j - 1) == '*') dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
else if (t.charAt(j - 1) == '?' || t.charAt(j - 1) == s.charAt(i - 1)) dp[i][j] = dp[i - 1][j - 1];
}
return dp[n][m];
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: bool isMatch(string s, string t) {
int n = s.size(), m = t.size();
vector<vector<bool>> dp(n + 1, vector<bool>(m + 1, false));
dp[0][0] = true;
for (int j = 1; j <= m; j++) if (t[j - 1] == '*') dp[0][j] = dp[0][j - 1];
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
if (t[j - 1] == '*') dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
else if (t[j - 1] == '?' || t[j - 1] == s[i - 1]) dp[i][j] = dp[i - 1][j - 1];
}
return dp[n][m];
}
};/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/ var isMatch = function(s, t) {
const n = s.length, m = t.length;
const dp = Array.from({length: n + 1}, () => new Array(m + 1).fill(false));
dp[0][0] = true;
for (let j = 1; j <= m; j++) if (t[j - 1] === '*') dp[0][j] = dp[0][j - 1];
for (let i = 1; i <= n; i++) for (let j = 1; j <= m; j++) {
if (t[j - 1] === '*') dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
else if (t[j - 1] === '?' || t[j - 1] === s[i - 1]) dp[i][j] = dp[i - 1][j - 1];
}
return dp[n][m];
};