Browse curriculum

Wildcard Matching (LeetCode 44)

Wildcard Matching (LeetCode 44): match a string against a pattern with '?' and '*' using a 2D DP table: a star matches the empty sequence or eats one more letter.

Problem Understanding

Wildcard Matching: match a string s against a pattern p where ? matches any single letter and * matches any sequence of letters, including the empty one. The match must cover the whole string.

Examples: s = "aa", p = "a" is false; p = "*" is true; s = "cb", p = "?a" is false.

The Intuition: A Star Either Stops or Eats One More

Ask whether the first i letters of s match the first j characters of p, and look at the last pattern character:

  1. A letter or ? must match s[i - 1], and the rest must already match: dp[i - 1][j - 1].
  2. A * that stops here matches the empty sequence: dp[i][j - 1].
  3. A * that eats one more takes s[i - 1] and stays available for earlier letters: dp[i - 1][j].

Case 3 is what lets one star cover any number of letters: each step up its column removes one more. Trying every length for every star by recursion repeats the same (i, j) pairs; the table solves each once. The same grid, with a star that repeats the previous character instead, solves Regular Expression Matching. Both are two-string dynamic programming like Edit Distance; for DP over ranges of one array, see Burst Balloons.

Wildcard Matching Visualization

EMPTY MATCHES EMPTY

∅

*

a

*

b

∅

T
F
F
F
F

a

F
F
F
F
F

d

F
F
F
F
F

c

F
F
F
F
F

e

F
F
F
F
F

b

F
F
F
F
F

cell

:

match

:

 

dp[0][0] = true: the empty pattern matches the empty string. Column 0 below it stays false

Next

'*' matches the empty sequence: dp[0][0] is true, so dp[0][1] = true

Free preview: the first 8 of 26 steps

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: diagonal, left (empty star) or up (star eats a letter). Try Near miss to see a pattern that almost fits.

Wildcard Matching Dry Run

Input: s = "adceb" (down), p = "*a*b" (across)

∅ * a * b
∅ T T F F F
a F T T T F
d F T F T F
c F T F T F
e F T F T F
b F T F T T
  • Column 1: the first * matches every prefix, so it is all T.
  • Column 3: the second * starts at row a (empty) and eats d, c, e, b going down.
  • Bottom-right: b matches and the diagonal dp[4][3] is T.

Answer: true: the first * is empty, the second covers "dce".

Wildcard Matching LeetCode Solution

A bottom-up table that runs row 0 too, so leading stars match the empty string.

Wildcard 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 for (let i = 0; i <= m; i++) {
6 for (let j = 1; j <= n; j++) {
7 const c = p[j - 1];
8 if (c === '*') {
9 dp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);
10 } else {
11 dp[i][j] = i > 0 && (c === '?' || c === s[i - 1]) && dp[i - 1][j - 1];
12 }
13 }
14 }
15 return dp[m][n];
16};

Wildcard Matching in Python, Java, C++, TypeScript and C

Wildcard 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 - 1] or (i > 0 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]
Wildcard 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++) {
                char c = p.charAt(j - 1);
                if (c == '*') {
                    dp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);
                } else {
                    dp[i][j] = i > 0 && (c == '?' || c == s.charAt(i - 1)) && dp[i - 1][j - 1];
                }
            }
        }
        return dp[m][n];
    }
}
Wildcard 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++) {
                char c = p[j - 1];
                if (c == '*') {
                    dp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);
                } else {
                    dp[i][j] = i > 0 && (c == '?' || c == s[i - 1]) && dp[i - 1][j - 1];
                }
            }
        }
        return dp[m][n];
    }
};
Wildcard 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;
    for (let i = 0; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            const c = p[j - 1];
            if (c === '*') {
                dp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);
            } else {
                dp[i][j] = i > 0 && (c === '?' || c === s[i - 1]) && dp[i - 1][j - 1];
            }
        }
    }
    return dp[m][n];
}
Wildcard 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++) {
            char c = p[j - 1];
            if (c == '*') {
                dp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);
            } else {
                dp[i][j] = i > 0 && (c == '?' || c == s[i - 1]) && dp[i - 1][j - 1];
            }
        }
    }
    return dp[m][n];
}

Edge Cases & Common Mistakes

  • Skipping row 0: a pattern of only stars matches the empty string; without row 0, s = "a", p = "**a" returns false.
  • Mixing up with regex: here * stands alone; its empty case is dp[i][j - 1], not dp[i][j - 2].
  • ? vs *: ? matches exactly one letter, never zero.
  • Partial matches: the whole string must match, so "a" does not match "aa".
  • Large C stack: with long inputs the C array dp[m + 1][n + 1] can overflow the stack; allocate it on the heap or keep one row.

Summary

Set dp[0][0] = true and fill every row, including the empty string. A letter or ? reads the diagonal; a * reads one to the left (empty) or straight up (eats a letter). Return dp[m][n]. O(m · n) time, and O(n) space with one row.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start the table

    dp[0][0] = true. Everything else starts false.

  2. Fill row by row, including row 0

    For a letter or '?', dp[i][j] = match(s[i - 1], p[j - 1]) and dp[i - 1][j - 1]. For '*', dp[i][j] = dp[i][j - 1] or dp[i - 1][j].

  3. Read the corner

    Return dp[m][n].

Frequently asked questions

What is the DP recurrence for Wildcard Matching?

dp[i][j] is true when the first i letters of s match the first j characters of p. If p[j - 1] is a letter or '?', it must match s[i - 1] and dp[i - 1][j - 1] must be true. If p[j - 1] is '*', dp[i][j] = dp[i][j - 1] (the star matches nothing) or dp[i - 1][j] (the star eats s[i - 1] and can keep going).


How is Wildcard Matching different from Regular Expression Matching?

In Wildcard Matching '*' stands alone and matches any sequence, so its empty case reads one cell left, dp[i][j - 1]. In Regular Expression Matching '*' repeats the character before it, so the pair 'x*' is skipped with dp[i][j - 2] and can only eat letters that match x.


Is there a faster solution than O(m · n)?

A greedy two-pointer method remembers the last '*' and the string position it was tried at, and backtracks there on a mismatch. It uses O(1) space and runs in O(m · n) worst case but is usually much faster. The DP table is easier to prove correct and is the standard interview answer.

New to the technique behind this problem? Watch it run step by step:

Dynamic Programming Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description