-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1688 from NK-Works/interleaving-strings
Added Interleaving Strings Algo
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Interleaving Strings | ||
|
||
## Problem Description | ||
Given three strings `A`, `B`, and `C`, determine if `C` is an interleaving of `A` and `B`. `C` is considered an interleaving of `A` and `B` if it contains all characters of `A` and `B`, with their order preserved, though characters from each string can be interwoven. | ||
|
||
### Example | ||
#### Example 1 | ||
- **Input:** | ||
A = "aab" B = "axy" C = "aaxaby" | ||
- **Output:** | ||
True | ||
|
||
- **Explanation:** | ||
`C` can be formed by interleaving `A` and `B`, as `C = "aaxaby"` follows the sequence of characters from both `A` and `B`. | ||
|
||
#### Example 2 | ||
- **Input:** | ||
A = "abc" B = "def" C = "abdecf" | ||
- **Output:** | ||
False | ||
- **Explanation:** | ||
`C` is not an interleaving of `A` and `B`, as the sequence of characters does not match both `A` and `B`. | ||
|
||
## Approach | ||
We use **Dynamic Programming (DP)** to solve this problem efficiently. We define a DP table where each cell `dp[i][j]` represents whether the first `i` characters of `A` and the first `j` characters of `B` can form the first `i + j` characters of `C`. | ||
|
||
### Key Insights | ||
1. If `A[i-1]` matches `C[i+j-1]`, then `dp[i][j]` depends on `dp[i-1][j]`. | ||
2. If `B[j-1]` matches `C[i+j-1]`, then `dp[i][j]` depends on `dp[i][j-1]`. | ||
3. If either condition above holds, then `dp[i][j]` is `true`. | ||
|
||
### DP Recurrence Relation | ||
- If `A[i-1] == C[i+j-1]`, then `dp[i][j] = dp[i-1][j]`. | ||
- If `B[j-1] == C[i+j-1]`, then `dp[i][j] = dp[i][j-1]`. | ||
- If either condition is true, `dp[i][j] = true`. | ||
|
||
### Steps | ||
1. Initialize a 2D DP table with dimensions `(n+1) x (m+1)`, where `n` is the length of `A` and `m` is the length of `B`. | ||
2. Fill the table based on the recurrence relations derived. | ||
3. The result will be in `dp[n][m]`, representing if all characters of `A` and `B` can interleave to form `C`. | ||
|
||
### Complexity Analysis | ||
- **Time Complexity**: `O(n * m)`, where `n` is the length of `A` and `m` is the length of `B`. | ||
- **Space Complexity**: `O(n * m)`, for storing the DP table. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
bool isInterleave(char* A, char* B, char* C) { | ||
int n = strlen(A); | ||
int m = strlen(B); | ||
int lenC = strlen(C); | ||
|
||
if (n + m != lenC) { | ||
return false; | ||
} | ||
|
||
bool dp[n + 1][m + 1]; | ||
|
||
dp[0][0] = true; // Empty A and B to form empty C is true | ||
|
||
for (int j = 1; j <= m; j++) { | ||
dp[0][j] = dp[0][j - 1] && (B[j - 1] == C[j - 1]); | ||
} | ||
|
||
for (int i = 1; i <= n; i++) { | ||
dp[i][0] = dp[i - 1][0] && (A[i - 1] == C[i - 1]); | ||
} | ||
|
||
for (int i = 1; i <= n; i++) { | ||
for (int j = 1; j <= m; j++) { | ||
dp[i][j] = (dp[i - 1][j] && A[i - 1] == C[i + j - 1]) || | ||
(dp[i][j - 1] && B[j - 1] == C[i + j - 1]); | ||
} | ||
} | ||
|
||
return dp[n][m]; | ||
} | ||
|
||
int main() { | ||
char A[] = "aab"; | ||
char B[] = "axy"; | ||
char C[] = "aaxaby"; | ||
|
||
if (isInterleave(A, B, C)) { | ||
printf("True\n"); | ||
} else { | ||
printf("False\n"); | ||
} | ||
|
||
return 0; | ||
} |