I'm trying to create an algorithm that after giving it a number of random letters, for example: abcd, it shows you all the possible permutations, in this case, the answer would be:
abcd abc abd acd ab ac ad a bcd bc bd b cd c d
As you can see, the different permutations must have different letters, I have spent hours trying to do it without success, this is what I did so far:
vector<char> letters;
letters.push_back('a');
letters.push_back('b');
letters.push_back('c');
letters.push_back('d');
int number_of_letters = 4;
int number_of_repetitions;
vector<char> combination;
vector<char> comb_copy;
for(int i = 0; i < number_of_letters; i++){
number_of_repetitions = 1;
changing_letters = number_of_letters - (i + 1);
for(int j = i+1; j < number_of_letters; j++){
combination.push_back(letters[j]);
}
comb_copy = combination;
for(int i = 0; i < comb_copy.size(); i++){
cout << comb_copy[i];
}
while(number_of_repetitions <= changing_letters){
comb_copy = combinations(comb_copy, number_of_repetitions);
for(int i = 0; i < comb_copy.size(); i++){
cout << comb_copy[i];
}
comb_copy = combination;
number_of_repetitions++;
}
}
vector<char> combinations(vector<char> combi, int reps){
combi.erase(combi.end() - reps);
return (reps > 1?combinations(combi, reps-1):combi);
}
And this is what I'm getting:
abcd abc abd acd bc bd c
I would need to get:
abcd abc abd acd ab ac ad a bcd bc bd b cd c d
Can someone help me? :)
Thanks!!