The problem: Given a string x, sort and then print all combinations. (found on a coding site such as hackerrank/interviewbit/geeksforgeeks)
Here is an example...
Input: string x = "BAC"
Output: [ABC, AB, AC, BC, A, B, C]
Current working solution: (works only for the case where x.length() = 3)
void printCombinations(string q){
stack<char> c;
stack<char> c2;
sort(q.begin(), q.end());
for(int i = 0; i<q.size(); i++){
cout<<q[i];
c.push(q[i]);
}
cout<<endl;
for(int i = 0; i<q.size(); i++){
char t = c.top();
cout<<t<<endl;
for(int j=0; j<c.size(); j++){
c.pop();
c2.push(c.top());
cout<< t << c.top() <<endl;
}
c.pop();
c = c2;
}
}