Change digits of an integer twice to maximize the difference between the largest and smallest possible values.
You are given a non-negative integer num. You may perform the following operation twice, independently:
num and replace every occurrence of that digit with another digit from 0 to 9.After one replacement, the resulting number must still be a valid integer representation; in particular, it cannot have leading zeros unless the number itself is 0.
Your goal is to maximize the difference between the value obtained by making a best possible replacement to create a large number and the value obtained by making a best possible replacement to create a small number.
Return that maximum difference.
num.You may treat it as a decimal string of digits for the purpose of replacement.
0 <= num <= $10^{8}$num contains no leading zeros unless it is exactly 0.Example 1
Input
num = 555
Output
888
Explanation
To maximize, change all 5 to 9 to get 999. To minimize, changing 5 to 1 gives 111. The difference is 999 - 111 = 888.
Example 2
Input
num = 9
Output
8
Explanation
The best maximum value is 9 itself. The smallest valid value is 1, obtained by changing 9 to 1. The difference is 8.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.