Description

Given a non-negative integer, you can swap at most one pair of digits. Return the maximum valued number you can get.

Examples

Input:num = 2736
Output:7236
Explanation:

Swap 2 and 7 to get 7236.

Input:num = 1993
Output:9913
Explanation:

Swap the first digit 1 with the largest digit 9 that appears later to get the maximum value. Choosing the rightmost 9 to swap with the 1 in position 0.

Input:num = 54321
Output:55321
Explanation:

The number is already in descending order except it is possible to improve it by swapping 4 with 5. Since 5 is already at the front, swapping the 4 (position 1) with the next largest digit that comes after it, which is 5 at position 4.

Constraints

  • 0 ≤ num ≤ 10⁸

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!