You are given an integer num. You will apply the following steps exactly two times:


	Pick a digit x (0 &lt;= x &lt;= 9).
	Pick another digit y (0 &lt;= y &lt;= 9). The digit y can be equal to x.
	Replace all the occurrences of x in the decimal representation of num by y.
	The new integer cannot have any leading zeros, also the new integer cannot be 0.


Let a and b be the results of applying the operations to num the first and second times, respectively.

Return the max difference between a and b.

&nbsp;
Example 1:

Input: num = 555
Output: 888
Explanation: 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


Example 2:

Input: num = 9
Output: 8
Explanation: 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


&nbsp;
Constraints:


	1 &lt;= num &lt;= 108


class Solution:
    def maxDiff(self, num: int) -> int:
        i=0
        while i<len(str(num)):
            change = (str(num)[i])
            if change!='9':
                break
            i+=1
        
        i=0
        flag = False
        while i<len(str(num)):
            sc = (str(num)[i])
            if sc!='1' and sc!='0':
                if i>0:
                    flag =True
                break
            i+=1
        small = str(num)
        num = str(num)
        m=''
        for i in range(len(str(num))):
            if num[i]==change:
                m+='9'
            else:
                m+=num[i]
        m = int(m)
        s =''
        for i in range(len(num)):
            if small[i]==sc:
                if flag:
                    s+='0'
                else:
                    if small[i]=="0":
                        s+='0'
                    else:
                        s+='1'
            else:
                s+=small[i]
        small = int(s)
        return m - small
class Solution {
    public int maxDiff(int num) {
        int[] arr = new int[String.valueOf(num).length()];
        for (int i = arr.length - 1; i >= 0; i--){
            arr[i] = num % 10;
            num /= 10;
        }
        return max(arr.clone()) - min(arr);
    }

    private int max(int[] arr){ // find max
        for (int i = 0, t = -1; i < arr.length; i++){
            if (t == -1 && arr[i] != 9){
                t = arr[i];
            }
            if (t == arr[i]){
                arr[i] = 9;
            }
        }
        return parse(arr);
    }

    private int min(int[] arr){ // find min
        int re = arr[0] == 1? 0 : 1;
        int t = arr[0] == 1? -1 : arr[0];
        for (int i = 0; i < arr.length; i++){
            if (t == -1 && arr[i] != 0 && arr[i] != arr[0]){
                t = arr[i];
            }
            if (t == arr[i]){
                arr[i] = re;
            }
        }
        return parse(arr);
    }

    private int parse(int[] arr){
        int ans = 0;
        for (int i = 0; i < arr.length; i++){
            ans = 10 * ans + arr[i];
        }
        return ans;
    }
}
class Solution {
public:
    int power10(int n)
    {
        int total=1;
        for(int i=0;i<n;i++)
        {
            total*=10;
        }
        return total;
    }
 
    int maxDiff(int num) {
    vector<int> v;
    vector<int> w;
    int n = num;
    while (n > 0)
    {
        v.push_back(n % 10);
        w.push_back(n % 10);
        n /= 10;
    }
        
    //Finding maximum number
    int d = v.size();
    int j = d - 1;
    for (int i = d - 1; i >= 0; i--)
    {
        if (v[i] != 9)
        {
            j = i;
            break;
        }
    }
    

    for (int i = 0; i <= j; i++)
    {
        if (v[i] == v[j])
        {
            v[i] = 9;
        }
    }
    long long int res = 0;
    for (int i = d - 1; i >= 0; i--)
    {
        res += (v[i] * power10(i));
    }

    //Finding minimum number   
    int t = w.size();
    if (w[t - 1] == 1)
    {
        int l = -1;
        for (int i = t - 2; i >= 0; i--)
        {
            if (w[i] != 0 && w[i]!=1)
            {
                l = i;
                break;
            }
        }
        for (int i = 0; i <= l; i++)
        {
            if (w[i] == w[l])
            {
                w[i] = 0;
            }
        }
    }
    else
    {
        for (int i = 0; i < t; i++)
        {
            if (w[i] == w[t - 1])
            {
                w[i] = 1;
            }
        }
    }
    long long int res2 = 0;
    for (int i = t - 1; i >= 0; i--)
    {
        res2 += (power10(i) * w[i]);
    }
    return res-res2;
    }
};
var maxDiff = function(num) {
    let occur = undefined;
    let max = num.toString().split("");
    let min = num.toString().split("");
    for(i=0;i<max.length;i++){
        if(max[i]<9&&!occur){
            occur = max[i];
            max[i] = 9;
        }
        if(max[i]===occur) max[i] = 9;
    }
    occur = undefined;
    let zerone;
     for(i=0;i<min.length;i++){
        if(!occur&&min[i]>1){
            occur = min[i];
            if(i===0) zerone = 1;
            else zerone = 0;
            min[i] = zerone;      
        }    
        if(min[i]===occur) min[i] = zerone;
    }
    return +max.join("")-+min.join("");
};
Max Difference You Can Get From Changing an Integer