#2566

Maximum Difference by Remapping a Digit

newbie · 230 · lc easy +17 · verified · 76.1% accepted · 608 likes · top 87%

Description

Given an integer num, Bob may remap any one digit globally — replacing every occurrence with another digit (possibly the same). Using one remapping to maximize and a separate remapping to minimize, return the difference between the maximum and minimum values achievable. Leading zeros in the result are permitted.

Example 1:

Input: num = 11891
Output: 99009
Explanation:
To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
The difference between these two numbers is 99009.

Example 2:

Input: num = 90
Output: 99
Explanation:
The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
Thus, we return 99.

Code

1
2
3