Back to Practice Dashboard
Top 150 InterviewMedium

Interleaving String

Learn how to solve the 'Interleaving String' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Medium

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 interleaved string is s1 + t1 + s2 + t2 + ... or t1 + s1 + t2 + s2 + ...

Write a function isInterleave(s1: str, s2: str, s3: str) -> bool.

Constraints
  • 0 <= len(s1), len(s2) <= 100
  • 0 <= len(s3) <= 200
  • s1, s2, and s3 consist of lowercase English letters

Examples

Example 1
Input
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output
True
Explanation

aadbbcbcac can be formed by interleaving "aabcc" and "dbbca".

Example 2
Input
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output
False
Explanation

It is impossible to interleave s1 and s2 to obtain s3.

Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor