Given a string, find and print all the possible permutations of the input string.

PROBLEM
:-
Print Permutations - String
Send Feedback

Given a string, find and print all the possible permutations of the input string.

Note : The order of permutations are not important. Just print them in different lines.
Sample Input :
abc
Sample Output :
abc
acb
bac
bca
cab
cba
SOLUTION:-
#include <iostream>
#include <string>
using namespace std;

#include <iostream>
#include <string>
using namespace std;


void P(string input,string output)
{
    if(input.empty())
    {
        cout<<output<<endl;
        return;
    }
    for(int i=0;i<input.size();i++)
    {
        P(input.substr(0,i)+input.substr(i+1),input.substr(i,1)+output);
    }
    return;
}
void printPermutations(string input){
     P(input,"");
    return;
}
int main(){
    string input;
    cin >> input;
    printPermutations(input);
    return 0;
}

Previous
Next Post »

If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon