Print Permutations
Print Permutations
Send Feedback
Print Permutations
The input string may contain the same characters, so there will also be the same permutations.
The order of permutations doesn’t matter.
The only input line contains a string (STR) of alphabets in lower case
Print each permutations in a new line
1<=length of STR<=8
Time Limit: 1sec
cba
abc
acb
bac
bca
cab
cba
#include <iostream>#include <string>using namespace std; void printPermutationsHelper(string input, string output){ if(input.length() == 0){ cout << output << endl; return; } for(int i = 0; i< input.length(); i++){ char t; t=input[0]; input[0]=input[i]; input[i]=t; printPermutationsHelper(input.substr(1), input[0]+output); }}void printPermutations(string input){ printPermutationsHelper(input, ""); return;}
#include <iostream>#include <string>using namespace std;
#include "solution.h"
int main() { string input; cin >> input; printPermutations(input); return 0;}
Comments
Post a Comment