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
Post a Comment