I found a link online that shows an algorithm to generate all combinations of a string: http://www.mytechinterviews.com/combinations-of-a-string
Algorithm is copied belo
Here is C++ code without the tricky backtracking step in OP's question.
#include
#include
using namespace std;
static const string in("abc");
void combine(int i, string out)
{
if (i==in.size()) {
cout << out << endl;
return;
}
combine(i+1, out);
combine(i+1, out+in[i]);
}
int main()
{
combine(0, "");
return 0;
}
I hope this better captures the spirit of combinations.