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

Return Permutations – String

PROBLEM:-Given a string S, find and return all the possible permutations of the input string.

Note 1 : The order of permutations is not important.

Note 2 : If original string contains duplicate characters, permutations will also be duplicates

Input Format :String S

Output Format :All permutations (in different lines)

Sample Input :abc

Sample Output :
abc
acb
bac
bca
cab
cba

SOLUTION:-

#include <iostream>
#include <string>
using namespace std;
#include <string>
using namespace std;
int returnPermutations(string input,string output[])
{
    if(input.size()==0)
    {
        output[0][0]=' ';
        return 1;
    }
    int p=0 ; 
    for(int i=0;i<input.size();i++)
    {
        string o1[10000] ; 
        string rem=input.substr( 0,i)+input.substr(i+1);
        int s1 = returnPermutations(rem,o1);
        for(int j=0;j<s1;j++)
        {
            output[p]=input[i]+o1[j];
            p+=1;
        }
    }
    return p; 
}




int main(){
    string input;
    cin >> input;
    string output[10000];
    int count = returnPermutations(input, output);
    for(int i = 0; i < count && i < 10000; i++){
        cout << output[i] << endl;
    }
    return 0;
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *