class Solution:
def maxDiff(self, num: int) -> int:
s = list(str(num))
# Find first digit that is not '9' for max
for d in s:
if d != '9':
max_num = int(''.join(['9' if x == d else x for x in s]))
break
else:
max_num = num
# Find first digit that is not '1' for min (first digit), else not '0' for others
if s[0] != '1':
d = s[0]
min_num = int(''.join(['1' if x == d else x for x in s]))
else:
for d in s[1:]:
if d != '0' and d != '1':
min_num = int(''.join([x if x != d else '0' for x in s]))
break
else:
min_num = num
return max_num - min_num
class Solution {
public:
int maxDiff(int num) {
string s = to_string(num);
string max_s = s, min_s = s;
// Maximize: replace first non-9 digit with 9
for (char d : s) {
if (d != '9') {
for (char &c : max_s) {
if (c == d) c = '9';
}
break;
}
}
// Minimize: replace first digit != 1 (if first), else next digit != 0/1 with 0
if (s[0] != '1') {
char d = s[0];
for (char &c : min_s) {
if (c == d) c = '1';
}
} else {
for (int i = 1; i < s.size(); ++i) {
if (s[i] != '0' && s[i] != '1') {
char d = s[i];
for (int j = 0; j < min_s.size(); ++j) {
if (min_s[j] == d) min_s[j] = '0';
}
break;
}
}
}
return stoi(max_s) - stoi(min_s);
}
};
class Solution {
public int maxDiff(int num) {
String s = Integer.toString(num);
char[] maxArr = s.toCharArray();
char[] minArr = s.toCharArray();
// Maximize: replace first non-9 digit with 9
for (char d : maxArr) {
if (d != '9') {
for (int i = 0; i < maxArr.length; ++i) {
if (maxArr[i] == d) maxArr[i] = '9';
}
break;
}
}
// Minimize: replace first digit != 1 (if first), else next digit != 0/1 with 0
if (minArr[0] != '1') {
char d = minArr[0];
for (int i = 0; i < minArr.length; ++i) {
if (minArr[i] == d) minArr[i] = '1';
}
} else {
for (int i = 1; i < minArr.length; ++i) {
if (minArr[i] != '0' && minArr[i] != '1') {
char d = minArr[i];
for (int j = 0; j < minArr.length; ++j) {
if (minArr[j] == d) minArr[j] = '0';
}
break;
}
}
}
int maxNum = Integer.parseInt(new String(maxArr));
int minNum = Integer.parseInt(new String(minArr));
return maxNum - minNum;
}
}
var maxDiff = function(num) {
let s = num.toString().split('');
let maxNum = num, minNum = num;
// Maximize: replace first non-9 digit with 9
for (let d of s) {
if (d !== '9') {
maxNum = parseInt(s.map(x => x === d ? '9' : x).join(''));
break;
}
}
// Minimize: replace first digit != 1 (if first), else next digit != 0/1 with 0
if (s[0] !== '1') {
let d = s[0];
minNum = parseInt(s.map(x => x === d ? '1' : x).join(''));
} else {
for (let i = 1; i < s.length; ++i) {
if (s[i] !== '0' && s[i] !== '1') {
let d = s[i];
minNum = parseInt(s.map(x => x === d ? '0' : x).join(''));
break;
}
}
}
return maxNum - minNum;
};
Given an integer num
, you are allowed to perform the following operation exactly once: pick a single digit (0-9) and replace all occurrences of that digit in num
with any other digit (0-9). You must perform this operation twice: once to maximize the resulting integer, and once to minimize it (note: the number cannot have leading zeros after the operation).
Your task is to compute and return the maximum difference between the largest and smallest values you can obtain by this process.
Constraints:
num
is positive (no negative numbers or zero).
At first glance, it might seem that we need to try all possible digit replacements for both maximizing and minimizing the number. For each digit (0-9), we could try replacing it with every other digit (also 0-9), and compute the resulting numbers, taking care to avoid leading zeros.
However, this brute-force approach would be inefficient, especially for numbers with many digits. Instead, we can reason about the optimal way to maximize and minimize the number:
Here’s how we can systematically solve the problem:
Let's walk through an example with num = 555
:
999
.111
.999 - 111 = 888
.
num = 9288
9988
.1288
.9988 - 1288 = 8700
.
Brute-force approach:
num
.
The key insight is to realize that for maximizing, we should replace the first non-'9' digit with '9', and for minimizing, the first digit (if not '1') with '1', or the first non-'0'/'1' digit (after the first) with '0'. This approach avoids brute-force enumeration and ensures optimal results by leveraging the structure of decimal numbers. The solution is efficient, elegant, and easy to implement in any language.