#K72502. Next Lexicographical Permutation
Next Lexicographical Permutation
Next Lexicographical Permutation
Given an integer (N) and a string representing a permutation of digits from 1 to (N), your task is to compute the next lexicographical permutation that is greater than the given permutation. If such a permutation does not exist (i.e. if the given permutation is the largest possible), output the smallest permutation (i.e. the digits sorted in increasing order).
For example, given (N = 3) and the permutation "231", the next permutation is "312".
inputFormat
The input is read from standard input (stdin) and consists of two tokens separated by whitespace. The first token is an integer (N) and the second token is a string representing a permutation of the digits from 1 to (N).
outputFormat
Output to standard output (stdout) the next lexicographical permutation as a string. If the given permutation is the largest, output the smallest permutation (i.e., sorted in increasing order).## sample
3 231
312