44. Wildcard Matching
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to implement a wildcard pattern matching algorithm that supports two special characters: ? and *. ? matches any single character, while * can match any sequence of characters, including an empty one. The matching must cover the entire input string s, not just a part of it.
Solution
Wildcard Matching in Python
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m, n = len(s), len(p)
# dp[j] will store the results for the current row i
# The size is n+1 to accommodate empty pattern
dp = [False] * (n + 1)
dp[0] = True # An empty pattern matches an empty string
# Handles patterns like '*', 'a*', '**' that can match an empty string s
for j in range(1, n + 1):
if p[j - 1] == '*':
dp[j] = dp[j - 1]
# Fill the DP table row by row for each character in s
for i in range(1, m + 1):
# prev stores dp[i-1][j-1]
prev = dp[0]
# dp[i][0] is always false for non-empty s
dp[0] = False
for j in range(1, n + 1):
# temp stores dp[i-1][j] to be used as 'prev' in the next iteration
temp = dp[j]
if p[j - 1] == '*':
# Two cases for '*':
# 1. '*' matches empty sequence: result is dp[i][j-1]
# 2. '*' matches one or more chars: result is dp[i-1][j]
# dp[j] becomes dp[i][j-1] or dp[i-1][j]
# Current dp[j] is dp[i-1][j], current dp[j-1] is dp[i][j-1]
dp[j] = dp[j] or dp[j - 1]
elif p[j - 1] == '?' or s[i - 1] == p[j - 1]:
# If characters match or pattern has '?', the result depends on the previous state
# dp[i][j] = dp[i-1][j-1]
dp[j] = prev
else:
# Characters do not match
dp[j] = False
# Update prev for the next j
prev = temp
return dp[n]
Wildcard Matching in Java
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
// dp[j] will be storing the results for the current row i
// The size is n+1 to accommodate empty pattern
boolean[] dp = new boolean[n + 1];
dp[0] = true; // An empty pattern matches an empty string
// Handles patterns like '*', 'a*', '**' that can match an empty string s
for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') {
dp[j] = dp[j - 1];
}
}
// Fill the DP table row by row for each character in s
for (int i = 1; i <= m; i++) {
// prev stores dp[i-1][j-1]
boolean prev = dp[0];
// dp[i][0] is always false for non-empty s
dp[0] = false;
for (int j = 1; j <= n; j++) {
// temp stores dp[i-1][j] to be used as 'prev' in the next iteration
boolean temp = dp[j];
if (p.charAt(j - 1) == '*') {
// Two cases for '*':
// 1. '*' matches empty sequence: result is dp[i][j-1]
// 2. '*' matches one or more chars: result is dp[i-1][j]
// dp[j] will be dp[i][j-1] || dp[i-1][j]
// Current dp[j] is dp[i-1][j], current dp[j-1] is dp[i][j-1]
dp[j] = dp[j] || dp[j - 1];
} else if (p.charAt(j - 1) == '?' || s.charAt(i - 1) == p.charAt(j - 1)) {
// If characters match or pattern has '?', the result depends on the previous state
// dp[i][j] = dp[i-1][j-1]
dp[j] = prev;
} else {
// Characters do not match
dp[j] = false;
}
// Update prev for the next j
prev = temp;
}
}
return dp[n];
}
}
Wildcard Matching in C++
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length();
int n = p.length();
// dp[j] will be storing the results for the current row i
// The size is n+1 to accommodate empty pattern
vector<bool> dp(n + 1, false);
dp[0] = true; // An empty pattern matches an empty string
// Handles patterns like '*', 'a*', '**' that can match an empty string s
for (int j = 1; j <= n; ++j) {
if (p[j - 1] == '*') {
dp[j] = dp[j - 1];
}
}
// Fill the DP table row by row for each character in s
for (int i = 1; i <= m; ++i) {
// prev stores dp[i-1][j-1]
bool prev = dp[0];
// dp[i][0] is always false for non-empty s
dp[0] = false;
for (int j = 1; j <= n; ++j) {
// temp stores dp[i-1][j] to be used as 'prev' in the next iteration
bool temp = dp[j];
if (p[j - 1] == '*') {
// Two cases for '*':
// 1. '*' matches empty sequence: result is dp[i][j-1]
// 2. '*' matches one or more chars: result is dp[i-1][j]
// dp[j] will be dp[i][j-1] || dp[i-1][j]
// Current dp[j] is dp[i-1][j], current dp[j-1] is dp[i][j-1]
dp[j] = dp[j] || dp[j - 1];
} else if (p[j - 1] == '?' || s[i - 1] == p[j - 1]) {
// If characters match or pattern has '?', the result depends on the previous state
// dp[i][j] = dp[i-1][j-1]
dp[j] = prev;
} else {
// Characters do not match
dp[j] = false;
}
// Update prev for the next j
prev = temp;
}
}
return dp[n];
}
};
Wildcard Matching in TypeScript
function isMatch(s: string, p: string): boolean {
const m = s.length;
const n = p.length;
// dp[j] will be storing the results for the current row i
// The size is n+1 to accommodate empty pattern
let dp: boolean[] = new Array(n + 1).fill(false);
dp[0] = true; // An empty pattern matches an empty string
// Handles patterns like '*', 'a*', '**' that can match an empty string s
for (let j = 1; j <= n; j++) {
if (p[j - 1] === '*') {
dp[j] = dp[j - 1];
}
}
// Fill the DP table row by row for each character in s
for (let i = 1; i <= m; i++) {
// prev stores dp[i-1][j-1]
let prev = dp[0];
// dp[i][0] is always false for non-empty s
dp[0] = false;
for (let j = 1; j <= n; j++) {
// temp stores dp[i-1][j] to be used as 'prev' in the next iteration
const temp = dp[j];
if (p[j - 1] === '*') {
// Two cases for '*':
// 1. '*' matches empty sequence: result is dp[i][j-1]
// 2. '*' matches one or more chars: result is dp[i-1][j]
// dp[j] will be dp[i][j-1] || dp[i-1][j]
// Current dp[j] is dp[i-1][j], current dp[j-1] is dp[i][j-1]
dp[j] = dp[j] || dp[j - 1];
} else if (p[j - 1] === '?' || s[i - 1] === p[j - 1]) {
// If characters match or pattern has '?', the result depends on the previous state
// dp[i][j] = dp[i-1][j-1]
dp[j] = prev;
} else {
// Characters do not match
dp[j] = false;
}
// Update prev for the next j
prev = temp;
}
}
return dp[n];
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.