Maximum Difference by Remapping a Digit
EasyUpdated: Aug 2, 2025
Practice on:
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 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 minimum and maximum values respectively.
- The resulting number after remapping can contain leading zeroes.
Examples
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.
Constraints
1 <= num <= 10^8
Solution
Method 1 – Greedy Digit Remapping
Intuition
To maximize the difference, remap a digit in num to 9 for the maximum value and to 0 for the minimum value. Try all possible digit remappings and pick the best results. Since only one digit can be remapped at a time (all its occurrences), brute-force all digit-to-digit remaps for both max and min.
Approach
- Convert
numto a string for easy digit manipulation. - For the maximum value:
- For each digit
dfrom0to9, replace all occurrences ofdwith9and record the resulting integer. - Track the largest value obtained.
- For the minimum value:
- For each digit
dfrom0to9, replace all occurrences ofdwith0and record the resulting integer. - Track the smallest value obtained.
- Return the difference between the maximum and minimum values.
Code
C++
class Solution {
public:
int minMaxDifference(int num) {
string s = to_string(num);
int mx = num, mn = num;
for (char d = '0'; d <= '9'; ++d) {
string t = s;
for (char &c : t) if (c == d) c = '9';
mx = max(mx, stoi(t));
}
for (char d = '0'; d <= '9'; ++d) {
string t = s;
for (char &c : t) if (c == d) c = '0';
mn = min(mn, stoi(t));
}
return mx - mn;
}
};
Go
func minMaxDifference(num int) int {
s := fmt.Sprintf("%d", num)
mx, mn := num, num
for d := byte('0'); d <= '9'; d++ {
t := []byte(s)
for i := range t {
if t[i] == d {
t[i] = '9'
}
}
v, _ := strconv.Atoi(string(t))
if v > mx {
mx = v
}
}
for d := byte('0'); d <= '9'; d++ {
t := []byte(s)
for i := range t {
if t[i] == d {
t[i] = '0'
}
}
v, _ := strconv.Atoi(string(t))
if v < mn {
mn = v
}
}
return mx - mn
}
Java
class Solution {
public int minMaxDifference(int num) {
String s = Integer.toString(num);
int mx = num, mn = num;
for (char d = '0'; d <= '9'; d++) {
String t = s.replace(d, '9');
mx = Math.max(mx, Integer.parseInt(t));
}
for (char d = '0'; d <= '9'; d++) {
String t = s.replace(d, '0');
mn = Math.min(mn, Integer.parseInt(t));
}
return mx - mn;
}
}
Kotlin
class Solution {
fun minMaxDifference(num: Int): Int {
val s = num.toString()
var mx = num
var mn = num
for (d in '0'..'9') {
val t = s.replace(d, '9')
mx = maxOf(mx, t.toInt())
}
for (d in '0'..'9') {
val t = s.replace(d, '0')
mn = minOf(mn, t.toInt())
}
return mx - mn
}
}
Python
class Solution:
def minMaxDifference(self, num: int) -> int:
s = str(num)
mx = num
mn = num
for d in '0123456789':
t = s.replace(d, '9')
mx = max(mx, int(t))
for d in '0123456789':
t = s.replace(d, '0')
mn = min(mn, int(t))
return mx - mn
Rust
impl Solution {
pub fn min_max_difference(num: i32) -> i32 {
let s = num.to_string();
let mut mx = num;
let mut mn = num;
for d in b'0'..=b'9' {
let t: String = s.bytes().map(|c| if c == d { b'9' } else { c }).map(|c| c as char).collect();
let v = t.parse::<i32>().unwrap();
if v > mx { mx = v; }
}
for d in b'0'..=b'9' {
let t: String = s.bytes().map(|c| if c == d { b'0' } else { c }).map(|c| c as char).collect();
let v = t.parse::<i32>().unwrap();
if v < mn { mn = v; }
}
mx - mn
}
}
Complexity
- ⏰ Time complexity:
O(k*10), wherekis the number of digits innum(since we try all 10 digits for both max and min). - 🧺 Space complexity:
O(k)for string manipulation.