Ctrl K

One Digit Swap

Medium
You are given a non-negative integer number. You may swap two of its digits at most once. Return the largest number you can get this way.
Example 1
number=4182 → 8142

Swap the 4 and the 8 to get 8142.

Example 2
number=8651 → 8651

The digits are already in descending order, so any swap would only make it smaller.

Example 3
number=1993 → 9913
Constraints
  • 0 ≤ number ≤ 10¹⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmallest Number From Digit Factors MediumLargest Number With Non-Decreasing Digits MediumFewest Guests by Lanyard Color MediumUp or Down for Every Post MediumDouble or Decrement Medium
loading Python…
Input
number=4182
Expected
8142

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.