</>

10. Regular Expression Matching

Hard NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
2-D Dynamic Programming
Time
—
Space
—

Standard Solution (C++)

SummaryGiven a string and a pattern that supports `.` (matches any single character) and `*` (matches zero or more of the preceding element), determine whether the pattern fully covers the entire string.

Approach
  • Build a 2-D DP table dp[i][j] meaning "the first i characters of s are fully matched by the first j characters of p."
  • Base case: dp[0][0] = true; a x* pair in p can collapse to zero occurrences, so pre-fill dp[0][j] = dp[0][j-2] when p[j-1] == '*'.
  • For each cell, if p[j-1] is a literal or ., carry over dp[i-1][j-1] when characters agree. If p[j-1] is *, either skip the pair (dp[i][j-2]) or, when the preceding pattern character matches s[i-1], extend a prior match (dp[i-1][j]).
  • The answer is dp[m][n] where m and n are the lengths of s and p.
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();

        // dp[i][j] = true iff s[0..i-1] is fully matched by p[0..j-1]
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));

        // Empty string matches empty pattern
        dp[0][0] = true;

        // A pattern like a*, a*b*, a*b*c* can match an empty string
        for (int j = 2; j <= n; j += 2) {
            if (p[j - 1] == '*') {
                dp[0][j] = dp[0][j - 2];
            }
        }

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                char pc = p[j - 1];

                if (pc == '*') {
                    // Option 1: treat "x*" as zero occurrences — skip the pair
                    dp[i][j] = dp[i][j - 2];

                    // Option 2: "x*" consumes one more s[i-1] if it matches
                    char prev = p[j - 2];
                    if (prev == '.' || prev == s[i - 1]) {
                        dp[i][j] = dp[i][j] || dp[i - 1][j];
                    }
                } else {
                    // Literal or '.': propagate diagonal if characters agree
                    if (pc == '.' || pc == s[i - 1]) {
                        dp[i][j] = dp[i - 1][j - 1];
                    }
                }
            }
        }

        return dp[m][n];
    }
};
Time O(m * n) Space O(m * n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    bool isMatch(string s, string p) {
        int slen=s.length();
        int plen=p.length();

        vector<vector<bool>> dp(slen+1, vector<bool>(plen+1, false)); // 1-based indice

        // initialize
        dp[0][0]=true;
        for(int j=1; j<=plen; ++j){
            if(p[j-1]=='*'){
                dp[0][j]=dp[0][j-2];
            }
        }

        for(int i=1; i<=slen; ++i){
            for(int j=1; j<=plen; ++j){
                if(p[j-1]!='*'){
                    if(p[j-1]=='.'||s[i-1]==p[j-1])
                        dp[i][j]=dp[i-1][j-1];
                }
                else{ // p[j]=='*'
                    // zero || one or more preceding element
                    dp[i][j]=dp[i][j-2];
                    if(s[i-1]==p[j-2]||p[j-2]=='.'){
                        dp[i][j]=dp[i][j]||dp[i-1][j];
                    }
                }
            }
        }

        return dp[slen][plen];
    }
};

// Time: O(M*N), where M is the length of s and N is the length of p
// Space: O(M*N)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern