I have this function:
void permutate (const string & s, std::vector<int>& index, std::size_t depth, int & count)
{
if (depth == s.size())
{
++count;
for (std::size_t i = 0; i < s.size(); ++i)
{
cout << s[index[i]];
}
cout << "\n";
return;
}
for (std::size_t i = 0; i < s.size(); ++i)
{
index[depth] = i;
permutate(s, index, depth + 1, count);
}
}
To print all combinations I use this:
void print(string s) {
if (s.find_first_not_of(s.front()) == string::npos)
{
cout << "Only 1 combination exists";
return;
}
sort(s.begin(), s.end());
cout << s << "\n**********\n";
vector<int> index(s.size());
int count = 0;
permutate(s, index, 0, count);
cout << "\nTotal combinations with repetitions: " << count;
}
Functions work great but I need specific combinations. For example, if I write
print("ABC");
I get: AAA, AAB, AAC, ABA, ..., CCA, CCB, CCC. So, I get overall 27 combination. But what if I need to make combination which size is not as the one of original set (in this case, size of original set is 3)?
For example, if I have a string "ABC" (or a set S = {A, B, C}) and I want combinations of size 2, I should get exactly 9 combinations: AA, AB, AC, BA, BB, BC, CA, CB, CC.
Please suggest changes that should be made in order to achieve my goal. Thank you.