-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrintInterleavingOfString.cpp
40 lines (33 loc) · 1.04 KB
/
PrintInterleavingOfString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Geeksforgeeks https://www.geeksforgeeks.org/print-all-interleavings-of-given-two-strings/
#include <iostream>
#include <string>
void printInterleavedStrings(const std::string& a, const std::string& b, std::string inter, int indexA, int indexB, int indexC)
{
if (indexA > a.size() && indexB > b.size()) {
return;
}
if (indexA == a.size() && indexB == b.size()) {
std::cout << inter << "\n";
}
if (indexA < a.size()) {
inter[indexC] = a[indexA];
printInterleavedStrings(a, b, inter, indexA + 1, indexB, indexC + 1);
}
if (indexB < b.size()) {
inter[indexC] = b[indexB];
printInterleavedStrings(a, b, inter, indexA, indexB + 1, indexC + 1);
}
}
void test(const std::string& a, const std::string& b)
{
std::cout << "a : " << a << ", b : " << b;
std::cout << "\nInterleaved string: \n";
std::string inter (a + b);
printInterleavedStrings(a, b, inter, 0, 0, 0);
std::cout << "=============================\n";
}
int main()
{
test("AB", "CD");
return 0;
}