You are viewing a single comment's thread. Return to all comments →
help me with this ..two cases cleared ..two to go....
#include<stdio.h> #include<string.h> #include<stdlib.h> int fun(char s[100],int j,int x,int l){ int flag=0; for(int i=x;i<l;i++){ if(s[i]>s[j]){ if(flag==0) flag=i; else if(s[i]<s[flag]) flag=i; } } return flag; } void sort(char s[100] ,int p,int n){ int i, j; for (i = p; i < n-1; i++) for (j = p; j < n-1; j++) if (s[j] > s[j+1]){ char temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++){ char s[100]; scanf("%s",s); int l=strlen(s); for(int j=l-2,x=j+1;j>=0;){ int flag=fun(s,j,x,l); if(flag!=0){ char temp=s[j]; s[j]=s[flag]; s[flag]=temp; sort(s,j+1,l); printf("%s\n",s); break; } else{ x++; if(x>=l){ j--; if(j<0){ printf("no answer\n"); break; } x=j+1; } } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Lexicographically Greater String
You are viewing a single comment's thread. Return to all comments →
help me with this ..two cases cleared ..two to go....