Given a string, I want to print all the combinations, picking letters from left to right: Ex. Input: abcd Output: abc acd abd bcd ac ad bc bd cd
I can do it, but i cannot generalize it, for ex. in the string abcd, i can get all the mentioned combinations by deleting only one letter. Then I can do it also by deleting two letters and so on. code:
name = "abcdefghi";
//Deleting 2 letters
for(int i = 1; i < name.size(); i++){
for(int k = 2; k < name.size(); k++){
for(int j = 0; j < name.size(); j++){ // PRINT ARRAY
if(j != i && j != k) cout << name[j];
}
cout << endl;
}
}
// Deleting 1 letter:
for(int i = 1; i < name.size(); i++){
for(int j = 0; j < name.size(); j++){ // PRINT ARRAY
if(j != i) cout << name[j];
}
cout << endl;
}
How can I generalize it so that I can first print the combination with 1 letter missing, then 2 letters missing, then 3, and so on... Because if I keep going like this, to get all the combinations with the number of letters missing from 1 to n, I will need n number of for loops...