I am trying to figure out how to print all the combinations in c++. Given input is {"abc","xyz"} and desired output is {"ax", "ay", "az", "bx", "by", "bz", "cx", "cy","cz"} I found this recursion code snippet :
`#include <bits/stdc++.h>
using namespace std;
void printKLengthString(char set[], string sequence, int n, int k) {
if (k == 0){
cout<<sequence<<"\t";
return;
}
for (int i = 0; i < n; i++){
string newSequence;
newSequence=sequence+set[i];
printKLengthString(set, newSequence, n, k - 1);
}
}
int main() {
char set[] = {'a', 'b'};
int n = 2;
int k = 3;
printKLengthString(set, "", n, k);
}`
but I am not able to manipulate it according to my desired inputs
Update 1: Here is my code:
`#include <bits/stdc++.h>
using namespace std;
void printKLengthString(vector<char> set, string sequence, int n, int k) {
if (k == 0){
cout<<sequence<<"\t";
return;
}
for (int i = 0; i < n; i++){
string newSequence;
newSequence=sequence+set.at(i);
printKLengthString(set, newSequence, n, k - 1);
}
}
int main() {
vector<string> stringIn = {"ab", "xy"};
// int n = 2;
// int k = 2;
// for (int i = 0; i < set.size(); i++) {
// cout << set[i] << "\n";
// }
vector<char> set;
for (int i = 0; i < stringIn.size(); i++) {
for (int j = 0; j < stringIn[0].size(); j++) {
// cout << stringIn[i].at(j) << "\n";
// str += char(set[i].at(j));
set.push_back(stringIn[i].at(j));
}
}
// for (char k: set) {
// cout << k << "\t";
// }
cout << "\n";
// cout << "stringIn Size : " << stringIn.size() << "\n";
// cout << "set Size : " << set.size() << "\n";
int k = stringIn.size();
int n = set.size();
printKLengthString(set, "", n, k);
}`
I am getting output as :
aa ab ax ay ba bb bx by xa xb xx xy ya yb yx yy
which is permutation but I just want the combination , which I am not able to figure out..
Anyone could guide me?
Update 2: I want to scale this for multiple inputs, e.g. {"abc","def","ghi","xyz"}