The String Interleaving Problem is a fascinating problem that is often encountered in algorithm interviews and dynamic programming discussions. At its core, the problem tests your understanding of how strings can be manipulated and combined, while adhering to specific rules.
Problem Definition
Given three strings: s1
, s2
, and s3
, we need to determine if s3
is formed by interleaving s1
and s2
. This means that all characters from s1
and s2
must be present in s3
, and the relative order of characters must be preserved.
For example, if we have:
s1 = "abc"
s2 = "def"
s3 = "adbcef"
In this case, s3
is indeed an interleaving of s1
and s2
: the character 'a' comes from s1
, 'd' from s2
, 'b' from s1
, 'c' from s1
, 'e' from s2
, and 'f' from s2
.
Conversely, if we had:
s1 = "abc"
s2 = "def"
s3 = "abdecf"
Here, s3
is not an interleaving of s1
and s2
because 'd' precedes 'e' unexpectedly, violating the order requirement.
Dynamic Programming Approach
To solve the String Interleaving Problem using dynamic programming (DP), we can create a 2D table where each cell dp[i][j]
indicates whether the first i
characters of s1
and the first j
characters of s2
can form the first i+j
characters of s3
.
Here's how you can visualize this:
-
Initialization:
- Create a DP table with dimensions
(len(s1) + 1) x (len(s2) + 1)
. - Set
dp[0][0]
toTrue
since an emptys1
ands2 can produce an empty
s3`.
- Create a DP table with dimensions
-
Filling the Table:
- Iterate through each character of
s1
ands2
, and fill indp
as follows:- If the current character of
s1
matches the corresponding character ins3
, carry over the value from the cell to the left (dp[i-1][j]
). - If the current character of
s2
matches the corresponding character ins3
, carry over the value from the cell above (dp[i][j-1]
).
- If the current character of
- The final value at
dp[len(s1)][len(s2)]
will indicate ifs3
can be formed by interleavings1
ands2
.
- Iterate through each character of
Example Code
Here’s a basic implementation in Python:
def is_interleave(s1: str, s2: str, s3: str) -> bool: if len(s1) + len(s2) != len(s3): return False dp = [[False] * (len(s2) + 1) for _ in range(len(s1) + 1)] dp[0][0] = True for i in range(1, len(s1) + 1): dp[i][0] = dp[i - 1][0] and s1[i - 1] == s3[i - 1] for j in range(1, len(s2) + 1): dp[0][j] = dp[0][j - 1] and s2[j - 1] == s3[j - 1] for i in range(1, len(s1) + 1): for j in range(1, len(s2) + 1): dp[i][j] = (dp[i - 1][j] and s1[i - 1] == s3[i + j - 1]) or \ (dp[i][j - 1] and s2[j - 1] == s3[i + j - 1]) return dp[len(s1)][len(s2)]
Explanation of the Code
- Edge Case: Initially, we check if the lengths of
s1
,s2
, ands3
are compatible. If not, we returnFalse
. - DP Table Creation: A table
dp
is constructed. The dimensions depend on the lengths ofs1
ands2
. - Filling Initial Rows and Columns: The first row and first column are filled based on whether characters in
s1
ands2
match withs3
. - Nested Loops: We then fill in each cell of the DP table based on previous results.
Time and Space Complexity
The time and space complexity for this algorithm is O(n * m), where n
is the length of s1
and m
is the length of s2
. This is because we are filling out a 2D table with dimensions related to these strings.
Conclusion
The String Interleaving Problem is a critical problem in dynamic programming, intricately tied to string manipulations. By understanding how to build the DP table and trace through the strings, we can tackle this problem efficiently. As you practice more dynamic programming problems, solutions like this will become second nature, enhancing your problem-solving skill set significantly.