Show problems as
Classic problem #2231EasySortingHeap (Priority Queue)

Largest Number After Digit Swaps by Parity

Real-world version: Swapping Digits That Share Their Oddness
Tracked in this browser only
Read Largest Number After Digit Swaps by Parity on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A counter shows the number num. Any two of its digits may be swapped, as often as you like, provided the two are both odd or both even.

Return the largest number the counter can be made to show.

Examples

Example 1

Input
num = 1357
Output
7531

Every digit is odd, so they can all be swapped with each other and falling order is largest.

Example 2

Input
num = 2468
Output
8642

Every digit is even, so again they arrange into falling order.

Example 3

Input
num = 1122
Output
1122

The two odd digits are both 1 and the two even digits are both 2, so no swap changes anything.

Constraints

  • 1 <= num <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def largest_integer(num: int) -> int:
Java
public int largestInteger(int num)