Maximum Difference by Remapping a Digit Problem & Solution
See the maximum difference by remapping a digit problem on LeetCode.
C++ Solution
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC taget("avx,avx2,fma")
#pragma GCC ivdep
static const int _=[]{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution {
public:
int minMaxDifference(int num) {
string high = to_string(num), low = high;
int i = 0;
char c = high[i];
while (i < high.size() && high[i] == '9') {
c = high[i];
++i;
}
c = i < high.size() ? high[i] : '9';
i = 0;
while ((i = high.find(c, i)) != string::npos) {
high.replace(i, 1, "9");
++i;
}
c = low.front();
i = 0;
while ((i = low.find(c, i)) != string::npos) {
low.replace(i, 1, "0");
++i;
}
return stoi(high) - stoi(low);
}
};