Maximum Difference by Remapping a Digit

You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.

Return the difference between the maximum and minimum 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 d1 in num with d2.
  • Bob can remap a digit to itself, in which case num does not change.
  • Bob can remap different digits for obtaining minimum and maximum values respectively.
  • The resulting number after remapping can contain leading zeroes.
Example 1
Inputnum = 11891
Output99009
Remapping digit 1 to 9 gives the maximum value 99899, while remapping digit 1 to 0 gives the minimum value 890, so the difference is 99009.
Example 2
Inputnum = 90
Output99
The maximum value is 99 by replacing 0 with 9, and the minimum value is 0 by replacing 9 with 0, so the difference is 99.

Constraints

  • 1 <= num <= 10^8

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate