You are given a positive integer num. You may swap any two digits of num that have the same parity, meaning both digits are odd or both are even. You may do this any number of times, including zero.
Return the largest possible value of num after any number of swaps.
Function Signature
def largest_integer(num: int) -> int:
Rules
-
A swap exchanges the digits at any two positions of the decimal form of
num
, adjacent or not. The two digits must both be odd or both be even. The digit
0
is even.
-
Return the resulting value as an integer.
Constraints
Examples
Input: num = 1234
Output: 3412
Swapping 1 with 3 (both odd) gives 3214, and swapping 2 with 4 (both even) then gives 3412. The first and third positions must hold odd digits and the second and fourth must hold even digits, so no larger value is reachable.
Input: num = 65875
Output: 87655
Swapping the 5 in the second position with the 7 (both odd) gives 67855, and swapping 6 with 8 (both even) then gives 87655.
Input: num = 206
Output: 620
All three digits are even, so they can be placed in any order, and 620 is the largest arrangement.