Skip to content
Decoder

Maximum Swap

Medium

Problem

You may swap two digits of the non-negative integer num at most once. Return the largest number you can get.

Example 1

Input
num = 2736
Output
7236

Example 2

Input
num = 9973
Output
9973
Why
already the largest, no swap

Example 3

Input
num = 1993
Output
9913

Constraints

  • 0 <= num <= 10^8

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.