All problems
1177EasySortingHeap (Priority Queue)

Swapping Digits That Share Their Oddness

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2231Largest Number After Digit Swaps by Parity

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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