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.
Going from right to left, stop as soon as the predecessor character is greater than the current character. Then find the rightmost such such character and swap. Finally, sort the characters to the right of the current character.
The effect of this procedure is to find the lowest possible lexicographic position which, when incremented by swapping, will be greater than the original string, and then to minimze all the lower positions. The resulting string is guaranteed to be lexicographically greater than the original, and any swap will be either greater than this string or smaller than the original string.
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
Going from right to left, stop as soon as the predecessor character is greater than the current character. Then find the rightmost such such character and swap. Finally, sort the characters to the right of the current character. The effect of this procedure is to find the lowest possible lexicographic position which, when incremented by swapping, will be greater than the original string, and then to minimze all the lower positions. The resulting string is guaranteed to be lexicographically greater than the original, and any swap will be either greater than this string or smaller than the original string.