We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<stdio.h>#include<stdlib.h>#include<string.h>voidswap(char**a,char**b){char*temp=*a;*a=*b;*b=temp;}intnext_permutation(intn,char**s){inti,j;// Find the rightmost string which is smaller than the string next to itfor(i=n-2;i>=0;i--){if(strcmp(s[i],s[i+1])<0)break;}// If no such string is found, then the array is the last permutationif(i<0)return0;// Find the smallest string on the right side of (i) that is greater than s[i]for(j=n-1;j>i;j--){if(strcmp(s[j],s[i])>0)break;}// Swap the found stringsswap(&s[i],&s[j]);// Reverse the substring to the right of (i)intleft=i+1,right=n-1;while(left<right){swap(&s[left],&s[right]);left++;right--;}return1;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Permutations of Strings
You are viewing a single comment's thread. Return to all comments →