check whether a string C is an interleaving of A and B
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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 |
Related reading
- Checking if a bit is set or not
- Checking if all elements of a vector are equal in C
- Checking if given preorder traversal is valid BST
- Checking if two strings are permutations of each other in Python
- checking subtrees using preorder and inorder strings
- Checksum for an integer array?
- Chess Bug in Alpha-Beta
- Chess Optimizations

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.