Problem with the code?

#include
#include
#include
using namespace std;
int find_index(string s,char key,int i,int j)
{
for(int k=j;k>=i;k–)
{
if(s[k]>key)
return k;
}
return -1;
}
void permutations(string s,int n)
{
int k;
for(k=n;s[k-1]>=s[k]:wink:
{
k–;
if(k==0)
return;
}
char first_element=s[k-1];
int index=find_index(s,first_element,k,n);
swap(s[k-1],s[index]);
sort(s.begin()+k,s.end());
cout<<s<<endl;
permutations(s,n);
}
int main()
{
string input;
cin>>input;
int n=input.size();
permutations(input,n-1);
return 0;
}

Hello @namangarg31,

Can you please explain your logic?

The way i approached it was bit simpler:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.