Maximum Difference by Remapping a Digit
You receive an integer num. You know that Bob will sneakily remap one of the 10 valid digits ( 0 to 9 ) to another digit.
Find and provide the difference between the largest and fewest values Bob can make by remapping exactly one digit in num.
Notes:
-
When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of
d1innumwithd2. -
Bob can remap a digit to itself, in which case
numdoes not change. -
Bob can remap different digits for obtaining fewest and largest values respectively.
-
The resulting number after remapping can contain leading zeroes.
Input: num = 11891
Output: 99009
Explanation: To achieve the largest value, Bob can remap the digit 1 to the digit 9 to yield 99899.
To achieve the fewest value, Bob can remap the digit 1 to the digit 0, yielding 890.
The difference between these two numbers is 99009.
Input: num = 90
Output: 99
Explanation: The largest value that can be returned by the function is 99 (if 0 is replaced by 9) and the fewest value that can be returned by the function is 0 (if 9 is replaced by 0).
Thus, we provide 99.
Constraints
1 <= num <= 10^8
Did this problem appear in one of your interviews?
Responses stay private.
