Maximum Difference by Remapping a Digit - Problem
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
d1to another digitd2, Bob replaces all occurrences ofd1innumwithd2. - Bob can remap a digit to itself, in which case
numdoes not change. - Bob can remap different digits for obtaining minimum and maximum values respectively.
- The resulting number after remapping can contain leading zeroes.
Input & Output
Example 1 — Basic Case
$
Input:
num = 123
›
Output:
900
💡 Note:
For maximum: replace 1→9 to get 923. For minimum: replace 1→0 to get 023 (which is 23). Difference is 923 - 23 = 900.
Example 2 — All Same Digits
$
Input:
num = 555
›
Output:
888
💡 Note:
For maximum: replace 5→9 to get 999. For minimum: replace 5→1 to get 111. Difference is 999 - 111 = 888.
Example 3 — Already Has 9
$
Input:
num = 9321
›
Output:
8019
💡 Note:
For maximum: replace 3→9 to get 9921. For minimum: replace 9→1 to get 1321. Difference is 9921 - 1321 = 8600.
Constraints
- 1 ≤ num ≤ 108
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given number that can have one digit remapped
2
Process
Find optimal digit replacements for max and min values
3
Output
Return difference between maximum and minimum possible values
Key Takeaway
🎯 Key Insight: Prioritize changing leftmost digits since they have the greatest impact on the number's value
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code