Input: num =555Output: 888Explanation: The first time pick x =5 and y =9 and store the new integer in a.The second time pick x =5 and y =1 and store the new integer in b.We have now a =999 and b =111 and max difference =888
Input: num =9Output: 8Explanation: The first time pick x =9 and y =9 and store the new integer in a.The second time pick x =9 and y =1 and store the new integer in b.We have now a =9 and b =1 and max difference =8
To maximize the difference, we want to create the largest and smallest possible numbers by replacing digits. For the maximum, replace the first non-9 digit with 9 everywhere. For the minimum, replace the first digit (if not 1) with 1, or the first non-0/1 digit with 0, ensuring no leading zeros.
classSolution {
public:int maxDiff(int num) {
string s = to_string(num);
string mx = s, mn = s;
// Maximize: replace first non-9 digit with 9
for (char c : s) {
if (c !='9') {
for (char&d : mx)
if (d == c) d ='9';
break;
}
}
// Minimize: replace first digit if not 1, else first non-0/1 digit with 0
if (s[0] !='1') {
char t = s[0];
for (char&d : mn)
if (d == t) d ='1';
} else {
for (int i =1; i < s.size(); ++i) {
if (s[i] !='0'&& s[i] !='1') {
char t = s[i];
for (char&d : mn)
if (d == t) d ='0';
break;
}
}
}
int a = stoi(mx), b = stoi(mn);
return a - b;
}
};
classSolution {
publicintmaxDiff(int num) {
String s = Integer.toString(num);
char[] mx = s.toCharArray();
char[] mn = s.toCharArray();
// Maximizefor (char c : mx) {
if (c !='9') {
for (int i = 0; i < mx.length; i++)
if (mx[i]== c) mx[i]='9';
break;
}
}
// Minimizeif (mn[0]!='1') {
char t = mn[0];
for (int i = 0; i < mn.length; i++)
if (mn[i]== t) mn[i]='1';
} else {
for (int i = 1; i < mn.length; i++) {
if (mn[i]!='0'&& mn[i]!='1') {
char t = mn[i];
for (int j = 0; j < mn.length; j++)
if (mn[j]== t) mn[j]='0';
break;
}
}
}
int a = Integer.parseInt(new String(mx));
int b = Integer.parseInt(new String(mn));
return a - b;
}
}
classSolution {
funmaxDiff(num: Int): Int {
val s = num.toString()
val mx = s.toCharArray()
val mn = s.toCharArray()
// Maximize
for (c in mx) {
if (c !='9') {
for (i in mx.indices)
if (mx[i] == c) mx[i] = '9'break }
}
// Minimize
if (mn[0] !='1') {
val t = mn[0]
for (i in mn.indices)
if (mn[i] == t) mn[i] = '1' } else {
for (i in1 until mn.size) {
if (mn[i] !='0'&& mn[i] !='1') {
val t = mn[i]
for (j in mn.indices)
if (mn[j] == t) mn[j] = '0'break }
}
}
val a = String(mx).toInt()
val b = String(mn).toInt()
return a - b
}
}
classSolution:
defmaxDiff(self, num: int) -> int:
s = str(num)
mx = list(s)
mn = list(s)
# Maximizefor c in s:
if c !='9':
mx = [ '9'if d == c else d for d in mx ]
break# Minimizeif s[0] !='1':
t = s[0]
mn = [ '1'if d == t else d for d in mn ]
else:
for i in range(1, len(s)):
if s[i] !='0'and s[i] !='1':
t = s[i]
mn = [ '0'if d == t else d for d in mn ]
break a = int(''.join(mx))
b = int(''.join(mn))
return a - b
impl Solution {
pubfnmax_diff(num: i32) -> i32 {
let s = num.to_string();
letmut mx = s.clone().into_bytes();
letmut mn = s.clone().into_bytes();
// Maximize
for&c in s.as_bytes() {
if c !=b'9' {
for d in mx.iter_mut() {
if*d == c { *d =b'9'; }
}
break;
}
}
// Minimize
if s.as_bytes()[0] !=b'1' {
let t = s.as_bytes()[0];
for d in mn.iter_mut() {
if*d == t { *d =b'1'; }
}
} else {
for i in1..s.len() {
let c = s.as_bytes()[i];
if c !=b'0'&& c !=b'1' {
for d in mn.iter_mut() {
if*d == c { *d =b'0'; }
}
break;
}
}
}
let a = String::from_utf8(mx).unwrap().parse::<i32>().unwrap();
let b = String::from_utf8(mn).unwrap().parse::<i32>().unwrap();
a - b
}
}