Wednesday 20 July 2016

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;
}

No comments:

Post a Comment

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-g...