check whether a string C is an interleaving of A and B
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In computer science, the concept of interleaving strings is a classic problem with applications in data processing, parsing, and concurrency control. The problem asks whether a string C is formed by interleaving two other strings A and B, preserving the character order within each source string. This article explains the problem, walks through the dynamic programming solution, and provides a complete worked example.
Problem Definition
A string C is an interleaving of strings A and B if C can be formed by merging A and B character by character, while maintaining the original left-to-right order of characters within both A and B.
For example, given:
A= "abc"B= "def"
Then C = "adbcef" is an interleaving of A and B (take a from A, d from B, b from A, c from A, e from B, f from B).
But C = "abdfce" is not valid because c and e appear out of their original order relative to B.
Necessary Conditions
Before running the full algorithm, two quick checks can reject invalid inputs:
- Length check: The length of
Cmust equal the sum of the lengths ofAandB:
- Character check: The multiset of characters in
Cmust equal the union of the multisets fromAandB. IfCcontains a character not present inAorB, or the wrong count, the answer is immediately false.
Dynamic Programming Solution
Define a 2D boolean table that represents whether the first characters of C can be formed by interleaving the first characters of A with the first characters of B.
Initialization
because an empty C is trivially an interleaving of empty A and empty B.
For the first row (): if matches .
For the first column (): if matches .
Transition
For each cell :
- If and is true, then
- If and is true, then
- Otherwise,
Result
The answer is .
Complexity
- Time: where and
- Space: for the full table, or using a rolling array that keeps only the current and previous rows
Worked Example
Let A = "aab", B = "axy", and C = "aaxaby".
First, check the length: . The check passes.
Build the DP table ():
| "" | a | x | y | |
| "" | T | T | F | F |
| a | T | T | T | F |
| a | F | T | F | F |
| b | F | T | T | T |
Reading , so "aaxaby" is an interleaving of "aab" and "axy".
Implementation
Space-Optimized Version
Since each row of the DP table only depends on the current row and the row above, you can reduce space to by using a single 1D array:
Edge Cases
- All three strings are empty: returns true.
- One of
AorBis empty:Cmust exactly equal the other string. AandBcontain identical characters (e.g.,A= "aa",B= "aa"): the DP correctly handles this since it tracks position within each source string independently.
Summary Table
| Aspect | Details | ||||||
| Problem | Determine if C is an interleaving of A and B | ||||||
| Key Condition | `$ | C | = | A | + | B | $` |
| Time Complexity | |||||||
| Space Complexity | , reducible to | ||||||
| Approach | Dynamic Programming | ||||||
| Initialization | |||||||
| Transition | Match or with |

