-
Notifications
You must be signed in to change notification settings - Fork 160
/
Allinterleaves.cpp
54 lines (41 loc) · 1.19 KB
/
Allinterleaves.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
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Find all the possible interleaves of two input strings.
For example:
input: ab, c
output: abc, acb, cab
*/
/*
solution: Recursion. Each time choose one char from str1 or str2, and store it in an output string, increase the index
for str1 or str2 and output string. When the length of output is sum of both string lengths, print output.
O(n1+n2) space
*/
#include<iostream>
#include<string>
using namespace std;
void AllInterleaves(string str1, string str2, string output,int index1,int index2, int &index) {
if (index == str1.size() + str2.size()) {
cout<<output<<endl;
return;
}
else {
if(index1 < str1.size()) {
output[index] = str1[index1];
int temp = index+1;
AllInterleaves(str1,str2,output,index1 + 1,index2, temp);
}
if(index2 < str2.size()) {
output[index] = str2[index2];
int temp = index+1;
AllInterleaves(str1,str2,output,index1,index2 + 1, temp);
}
}
}
int main() {
string str1 = "abc";
string str2 = "de";
int len = str1.size() + str2.size();
string output(len, ' ');
int index = 0;
AllInterleaves(str1,str2,output,0,0,index);
return 0;
}