</>

97. Interleaving String

Medium NeetCode 150
2-D Dynamic Programming Time O(M*N) Space O(min(M, N)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
2-D Dynamic Programming
Time
O(M*N)
Space
O(min(M, N)

Standard Solution (C++)

SummaryGiven two strings s1 and s2, determine whether a third string s3 can be formed by weaving together characters from s1 and s2 while preserving each string's original character order.

Approach
  • Define dp[i][j] as true when s3[0..i+j-1] is a valid interleaving of s1[0..i-1] and s2[0..j-1].
  • Transition: dp[i][j] is true if either (a) dp[i-1][j] is true and s1[i-1] == s3[i+j-1], or (b) dp[i][j-1] is true and s2[j-1] == s3[i+j-1].
  • Space-optimize to a single 1-D array of length len(s2)+1 by rolling over the s1 dimension, reusing each cell in-place.
  • Base case seeds the first row/column along the edges where only one source string has been consumed.
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        int m = s1.size(), n = s2.size();
        if (m + n != (int)s3.size()) return false;

        // dp[j] = can s3[0..i+j-1] be formed from s1[0..i-1] and s2[0..j-1]
        // We iterate over s1 (outer) and s2 (inner), rolling the i-dimension.
        vector<bool> dp(n + 1, false);

        // Base: consume only s2 characters (i == 0)
        dp[0] = true;
        for (int j = 1; j <= n; ++j)
            dp[j] = dp[j - 1] && (s2[j - 1] == s3[j - 1]);

        for (int i = 1; i <= m; ++i) {
            // dp[0] for this row: consume only s1[0..i-1], no s2
            dp[0] = dp[0] && (s1[i - 1] == s3[i - 1]);

            for (int j = 1; j <= n; ++j) {
                // take from s1: dp[j] (previous row, same j)
                bool fromS1 = dp[j]     && (s1[i - 1] == s3[i + j - 1]);
                // take from s2: dp[j-1] (same row, previous j)
                bool fromS2 = dp[j - 1] && (s2[j - 1] == s3[i + j - 1]);
                dp[j] = fromS1 || fromS2;
            }
        }

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

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

My Solution (C++)

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        // use dp, dp[i][j] means whether s3[0:i+j-1] is an interleaving of s1[0:i-1] and s2[0:j-1]
        int len1=s1.length();
        int len2=s2.length();
        int len3=s3.length();

        if(len1+len2!=len3){
            return false;
        }

        if(len1>len2){
            return isInterleave(s2, s1, s3);
        }

        vector<bool> dp(len1+1, false);

        // init
        dp[0]=true;
        for(int i=1; i<=len1; ++i){
            dp[i]=(dp[i-1] && s1[i-1]==s3[i-1]);
        }

        for(int j=1; j<=len2; ++j){
            dp[0]=(dp[0] && s2[j-1]==s3[j-1]);
            for(int i=1; i<=len1; ++i){
                dp[i]=(dp[i-1] && s1[i-1]==s3[i+j-1])||(dp[i] && s2[j-1]==s3[i+j-1]);
            }
        }

        return dp[len1];
    }
};

// Time Complexity: O(M*N)
// Space Complexity: O(min(M, N))
Time O(M*N) Space O(min(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