the last test case is passing only if i print the answer after the recursive i.e. in decreasing order unlike other test cases…i think order of the answer of the last test case is wrong…please see it…
and similarily for dictionary order (smaller) the output of sample test case is not in dictionary order…
here is my code for dictionary order (larger)—
#include<bits/stdc++.h>
using namespace std;
int search(string str,int s,int key){
int e=str.length()-1;
int mid,ans;
while(s<=e){
mid=(s+e)/2;
if(str[mid]<=key)
e=mid-1;
else{
ans=mid;
s=mid+1;
}
}
return ans;
}
void next(string s){
int i=s.length()-2;
while(i>=0&&s[i]>=s[i+1])
i--;
if(i<0)
return ;
else{
int j=search(s,i+1,s[i]);
swap(s[i],s[j]);
reverse(s.begin()+(i+1),s.end());
cout<<s<<endl;
next(s);
}
}
int main(){
string s;
cin>>s;
next(s);
return 0;
}