#include
#include
#include
using namespace std;
void dictionary(char*inp,int b)
{ char out[11];
int i=0,l=0;
int j=0,k=0;
while(inp[i]!=β\0β)
{
out[j]=inp[i];
i++;
j++;
}
out[j]=β\0β;
int digit;
next_permutation(inp,inp+b);
if(strcmp(out,inp)>=0)
{
return;
}
while(inp[k]!=β\0β)
{
digit=inp[k]-out[l];
if(digit>0)
{
cout<<inp<<" ";
break;
}
k++;
l++;
}
dictionary(inp,b);
}
int main() {
char inp[11];
cin>>inp;
char out[11];
int a=0,b=0;
while(inp[a]!=β\0β)
{
b++;
a++;
}
dictionary(inp,b);
return 0;
}
What is wrong with the code
Hello @Kunalgoyal,
Your code is causing segmentation error for larger strings like:
qwertyuio
Also, the output format of your code does not matches with that of the question.
Each permutation should be in a separate line.
Simpler way of solving this question:
Your code is showing all permutations except the original one. But we have to show lexographically greater
Hey @Kunalgoyal,
No, thatβs not possible.
Look at the condition inside the base case:
I am storing only those permutations that are greater than the given string.
And to maintain the lexographical order, I am sorting the vector.
Please, check it again or give me an example in support of your observation.
u can check by putting cab.
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.
