Watch 10 video solutions for Interleaving String, a medium level problem involving String, Dynamic Programming. This walkthrough by NeetCode has 112,299 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
An interleaving of two strings s and t is a configuration where s and t are divided into n and m substrings respectively, such that:
s = s1 + s2 + ... + snt = t1 + t2 + ... + tm|n - m| <= 1s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...Note: a + b is the concatenation of strings a and b.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Explanation: One way to obtain s3 is: Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a". Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac". Since s3 can be obtained by interleaving s1 and s2, we return true.
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" Output: false Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
Example 3:
Input: s1 = "", s2 = "", s3 = "" Output: true
Constraints:
0 <= s1.length, s2.length <= 1000 <= s3.length <= 200s1, s2, and s3 consist of lowercase English letters.
Follow up: Could you solve it using only O(s2.length) additional memory space?
Problem Overview: You are given three strings s1, s2, and s3. The goal is to determine whether s3 can be formed by interleaving characters from s1 and s2 while preserving the original order of characters in both strings.
The key constraint is order preservation. You can switch between the two strings at any time, but characters from each string must appear in the same relative order. This naturally leads to exploring combinations of prefixes from both strings.
Approach 1: Recursive with Memoization (Top-Down DP) (Time: O(m*n), Space: O(m*n))
Think of the problem as building s3 character by character while choosing whether the next character comes from s1 or s2. Use recursion with two indices i and j representing how many characters have been consumed from s1 and s2. The next character in s3 is at position i + j. If s1[i] matches s3[i+j], recursively move to (i+1, j). If s2[j] matches, move to (i, j+1).
Without caching, the recursion explores the same states many times. Memoization stores results for each state (i, j), avoiding recomputation. This reduces the exponential search space to a grid of size m × n. This approach is intuitive because it directly models the decision process and works well when explaining the logic during interviews involving dynamic programming and recursion.
Approach 2: Dynamic Programming (Bottom-Up) (Time: O(m*n), Space: O(m*n))
The bottom-up DP solution builds a table dp[i][j] where each cell indicates whether the first i characters of s1 and the first j characters of s2 can form the prefix s3[0..i+j-1]. Initialize dp[0][0] as true since empty strings form an empty interleaving.
Fill the table by iterating over all i and j. If the current character of s1 matches s3, propagate the value from dp[i-1][j]. If the character from s2 matches, propagate from dp[i][j-1]. Each state checks whether either path leads to a valid interleaving.
This transforms the problem into a grid traversal where each cell depends on the top and left neighbors. The approach is deterministic, avoids recursion overhead, and is typically preferred for production code involving string processing and DP state transitions.
Recommended for interviews: Interviewers usually expect the dynamic programming formulation with a dp[i][j] state. Starting with the recursive idea demonstrates understanding of the decision tree, and then optimizing it with memoization or converting it to bottom-up DP shows strong problem-solving skills. The bottom-up DP approach is the cleanest final solution with predictable O(m*n) time and space complexity.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive with Memoization | O(m*n) | O(m*n) | Best for understanding the decision tree and explaining the logic step-by-step in interviews |
| Dynamic Programming (Bottom-Up) | O(m*n) | O(m*n) | Preferred production solution with predictable iteration and no recursion overhead |
| Dynamic Programming (Space Optimized) | O(m*n) | O(n) | Useful when memory is constrained since only the previous row of DP states is required |