Skip to main content

97. Interleaving String

medium

Given strings s1, s2, s3, decide whether s3 is formed by interleaving the characters of s1 and s2 while preserving each string's relative order. Two pointers, two recursive branches — memoization saves you from exponential blow-up.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

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 they are divided into non-empty substrings such that: s = s1 + s2 + ... + sn, t = t1 + t2 + ... + tm, |n - m| <= 1, and the interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ... Note: a + b is the concatenation of strings a and b.

Constraints

  • 0 <= s1.length, s2.length <= 100
  • 0 <= s3.length <= 200
  • s1, s2, and s3 consist of lowercase English letters.

Examples

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

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

If s1.length + s2.length != s3.length, return false.

Hint 2

Recurse with (i, j) — pointers into s1 and s2. The pointer into s3 is i + j.

Hint 3

At (i, j): if s1[i] == s3[i+j], try recursing (i+1, j). If s2[j] == s3[i+j], try recursing (i, j+1). Either success returns true.

Hint 4

Memoize on (i, j) — there are O(m * n) states.

Solution approach

Reveal approach

If s1.length + s2.length != s3.length, return false. Define match(i, j) = can we form s3[0..i+j-1] from s1[0..i-1] and s2[0..j-1]? Base case: i == s1.length and j == s2.length -> return true. Recursive: result is false; if i < s1.length and s1[i] == s3[i + j], result = match(i + 1, j); if !result and j < s2.length and s2[j] == s3[i + j], result = match(i, j + 1). Memoize on (i, j). Iterative DP: dp[i][j] = match for prefixes of length i, j. dp[0][0] = true. dp[i][j] = (i > 0 and dp[i-1][j] and s1[i-1] == s3[i+j-1]) OR (j > 0 and dp[i][j-1] and s2[j-1] == s3[i+j-1]). Time and space O(m * n); the row-based DP gives O(min(m, n)) space.

Complexity

Time
O(m * n)
Space
O(m * n)

Related patterns

  • recursion
  • memoization
  • dynamic-programming

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Microsoft
  • Apple

Practice these live with InterviewChamp.AI

Drill Interleaving String and Recursion problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →