Printing all the permutation of the string using recursion


           dog
   dog ,  odg ,  god    (in first call 1st character is swapped with all postions (1st to last))
dgo     ogd       gdo   (in 2nd call ,2nd character is swapped with all postions(2nd to last) )




#include<bits/stdc++.h>

using namespace std;

void permu(string k,int fst,int lst)
{
    if(fst==lst)
        cout<<k<<endl;

    for(int i=fst;i<=lst;i++)
    {
        swap(k[fst],k[i]);
        permu(k,fst+1,lst);
        swap(k[fst],k[i]);
    }
}

int main()
{
    string s;
    cin>>s;
    int lst=s.size()-1;
    permu(s,0,lst);
    return 0;
}

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.