P16094 [GeeksforGeeks] Previous number in one swap5707
普及
题目描述

Given a non-negative number N in the form of string. The task is to apply at most one swap operation on the number N so that the result is just a previous possible number.
Note: Leading zeros are not allowed.

Example 1:
Input :
S = "12435"
Output:
12345
Explanation:
Although the number 12354
will be the largest smaller
number from 12435. But it is
not possible to make it using
only one swap. So swap
4 and 3 and get 12345.

Example 2:
Input:
S = " 12345"
Output:
-1
Explanation:
Digits are in increasing order.
So it is not possible to
make a smaller number from it.

Your Task:
You don't need to read input or print anything. Your task is to complete the function previousNumber() which takes the string S and returns the previous number of S. If no such number exists return -1;

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
2<=|Number length |<=10^{5}

输入格式

见题面

输出格式

见题面

样例输入
S = "12435"
样例输出
12345