Largest Number After Digit Swaps by Parity

You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits).

Return the largest possible value of num after any number of swaps.

Example 1
Inputnum = 1234
Output3412
By swapping digits only with digits of the same parity, 3412 is the largest possible number.
Example 2
Inputnum = 65875
Output87655
By swapping digits only with digits of the same parity, 87655 is the largest possible number.

Constraints

  • 1 <= num <= 10^9

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate