↻one more pass.
2D dynamic programming · Hard

Regular Expression Matching: the idea to remember.

Match an entire string against a pattern where dot matches one character and star repeats the preceding token zero or more times.

Solve on LeetCode ↗

The core insight

For a starred token, try skipping its pair or consuming one matching character while keeping the token available.

Why it works

Every use of a starred token is either zero repetitions or at least one followed by the same remaining repetition problem.

The mistake to avoid

A star repeats its preceding token; it is not an independent wildcard. Matching must consume the whole string and pattern.

Time: O(text length · pattern length)Space: O(text length · pattern length)

Commented Java solution

Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.

boolean isMatch(String text, String pattern) {
    return match(text, pattern, 0, 0, new Boolean[text.length()+1][pattern.length()+1]);
}
boolean match(String text, String pattern, int i, int j, Boolean[][] memo) {
    if (memo[i][j] != null) return memo[i][j]; // Cache false results too.
    if (j == pattern.length()) return memo[i][j] = (i == text.length());
    boolean first = i < text.length() && (pattern.charAt(j) == '.' || text.charAt(i) == pattern.charAt(j));
    if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*')
        return memo[i][j] = match(text,pattern,i,j+2,memo) || (first && match(text,pattern,i+1,j,memo));
    return memo[i][j] = first && match(text,pattern,i+1,j+1,memo);
}

Make the next connection

Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.