Skip to content

Commit

Permalink
Merge pull request #1688 from NK-Works/interleaving-strings
Browse files Browse the repository at this point in the history
Added Interleaving Strings Algo
  • Loading branch information
pankaj-bind authored Nov 6, 2024
2 parents b9badb8 + b9d6d7a commit 235ce7a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Dynamic Programming/Interleaving Strings/README.md
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.
48 changes: 48 additions & 0 deletions Dynamic Programming/Interleaving Strings/program.c
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;
}

0 comments on commit 235ce7a

Please sign in to comment.