Skip to content

Commit b9a1657

Browse files
committed
interleaving string added
1 parent f9cb14f commit b9a1657

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

Diff for: Backtracking

Whitespace-only changes.

Diff for: Backtracking : N Queens Problem

-69
This file was deleted.

Diff for: Interleaving_string.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <string.h>
3+
using namespace std;
4+
5+
bool isInterleaved(
6+
char* A, char* B, char* C)
7+
{
8+
int M = strlen(A), N = strlen(B);
9+
10+
bool IL[M + 1][N + 1];
11+
12+
memset(IL, 0, sizeof(IL));
13+
14+
if ((M + N) != strlen(C))
15+
return false;
16+
17+
for (int i = 0; i <= M; ++i) {
18+
for (int j = 0; j <= N; ++j) {
19+
20+
if (i == 0 && j == 0)
21+
IL[i][j] = true;
22+
23+
else if (i == 0) {
24+
if (B[j - 1] == C[j - 1])
25+
IL[i][j] = IL[i][j - 1];
26+
}
27+
28+
else if (j == 0) {
29+
if (A[i - 1] == C[i - 1])
30+
IL[i][j] = IL[i - 1][j];
31+
}
32+
33+
else if (
34+
A[i - 1] == C[i + j - 1]
35+
&& B[j - 1] != C[i + j - 1])
36+
IL[i][j] = IL[i - 1][j];
37+
38+
else if (
39+
A[i - 1] != C[i + j - 1]
40+
&& B[j - 1] == C[i + j - 1])
41+
IL[i][j] = IL[i][j - 1];
42+
43+
else if (
44+
A[i - 1] == C[i + j - 1]
45+
&& B[j - 1] == C[i + j - 1])
46+
IL[i][j]
47+
= (IL[i - 1][j]
48+
|| IL[i][j - 1]);
49+
}
50+
}
51+
52+
return IL[M][N];
53+
}
54+
55+
void test(char* A, char* B, char* C)
56+
{
57+
if (isInterleaved(A, B, C))
58+
cout << C << " is interleaved of "
59+
<< A << " and " << B << endl;
60+
else
61+
cout << C << " is not interleaved of "
62+
<< A << " and " << B << endl;
63+
}
64+
65+
int main()
66+
{
67+
test("XXY", "XXZ", "XXZXXXY");
68+
test("XY", "WZ", "WZXY");
69+
test("XY", "X", "XXY");
70+
test("YX", "X", "XXY");
71+
test("XXY", "XXZ", "XXXXZY");
72+
return 0;
73+
}

0 commit comments

Comments
 (0)