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.
thanks. copied this solution and refactored a little bit.
staticStringbiggerIsGreater(Stringw){char[]chars=w.toCharArray();returnnextPermutation(chars)?newString(chars):"no answer";}staticbooleannextPermutation(char[]array){inti=getLastPeakIndex(array);if(i<=0)// like "dcba", no answerreturnfalse;intj=getReplacementIndex(array,i-1);swap(array,i-1,j);// Reverse the part on the right of i - 1 to make it as small as possiblej=array.length-1;while(i<j){swap(array,i,j);i++;j--;}returntrue;}staticintgetLastPeakIndex(char[]chars){inti=chars.length-1;while(i>0&&chars[i-1]>=chars[i])i--;returni;}staticintgetReplacementIndex(char[]chars,intgivenIndex){inti=chars.length-1;while(chars[i]<=chars[givenIndex])i--;returni;}staticvoidswap(char[]chars,inta,intb){chars[a]^=chars[b];chars[b]^=chars[a];chars[a]^=chars[b];}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
thanks. copied this solution and refactored a little bit.