#include
using namespace std;
void lexographical(int &k,string *temp){
string str;
for(int i=1;i<k;i++){
for(int j=i+1;j<=k;j++){
if(temp[i]>temp[j]){
str=temp[i];
temp[i]=temp[j];
temp[j]=str;
}
}
}
return;
}
void permute(char *a,int i,string *temp,int &k){
if(a[i]==β\0β){
char s[1000];
if(a>temp[0]){
k++;
temp[k]=a;
}
return;
}
for(int j=i;a[j]!=0;j++){
swap(a[i],a[j]);
permute(a,i+1,temp,k);
swap(a[i],a[j]);
}
}
int main(){
char a[1000];
int k=0;
string temp[10000];
cin>>a;
temp[0]=a;
permute(a,0,temp,k);
lexographical(k,temp);
for(int i=1;i<=k;i++){
cout<<temp[i]<<endl;
}
if(k==0){
cout<<a;
}
return 0;
}
Why my code is wrong?
Hello @abheysingla2310,
I have corrected your code and now it is passing all the test cases:
Refer to comments for better understanding.
Let me know if you donβt understand something.
Hope, this would help.
Give a like if you are satisfied.
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.