Interleaving String
Learn how to solve the 'Interleaving String' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
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.
- •0 <= len(s1), len(s2) <= 100
- •0 <= len(s3) <= 200
- •s1, s2, and s3 consist of lowercase English letters
Examples
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
True
aadbbcbcac can be formed by interleaving "aabcc" and "dbbca".
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
False
It is impossible to interleave s1 and s2 to obtain s3.
Need a Hint?
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.