package leetcode;

/**
 * https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/
 */
public class Problem1432 {
    public int maxDiff(int num) {
        String s = Integer.toString(num);
        int i = 0;
        while (i < s.length() && s.charAt(i) == '9') {
            i++;
        }
        StringBuilder max = new StringBuilder();
        if (i == s.length()) {
            i = 0;
        }
        for (int j = 0; j < s.length(); j++) {
            if (s.charAt(i) == s.charAt(j)) {
                max.append("9");
            } else {
                max.append(s.charAt(j));
            }
        }
        StringBuilder min = new StringBuilder();
        i = 0;
        boolean zero = false;
        if (s.charAt(i) == '1') {
            while (i < s.length() && s.charAt(i) == '1') {
                i++;
            }
            if (i == s.length()) {
                i = 0;
            } else {
                zero = true;
            }
        }

        for (int j = 0; j < s.length(); j++) {
            if (s.charAt(i) == s.charAt(j)) {
                min.append(zero ? "0" : "1");
            } else {
                min.append(s.charAt(j));
            }
        }
        System.out.println(max + " " + min);
        return Integer.parseInt(max.toString()) - Integer.parseInt(min.toString());
    }

    public static void main(String[] args) {
        Problem1432 prob = new Problem1432();
//        System.out.println(prob.maxDiff(555)); // 888
//        System.out.println(prob.maxDiff(9)); // 8
//        System.out.println(prob.maxDiff(123456)); // 820000
//        System.out.println(prob.maxDiff(10000)); // 80000
//        System.out.println(prob.maxDiff(9288)); // 8700
//        System.out.println(prob.maxDiff(9289)); // 8708
//        System.out.println(prob.maxDiff(1111)); // 8888
//        System.out.println(prob.maxDiff(9999)); // 8888
//        System.out.println(prob.maxDiff(1000)); // 8000
//        System.out.println(prob.maxDiff(3)); // 8
        System.out.println(prob.maxDiff(1101057)); // 8808050
    }
}

@@ -787,32 +787,55 @@
  %3C s.length() &&
+ (s.charAt(i) == '0' %7C%7C
  s.charAt(i) == 
@@ -830,32 +830,33 @@
 harAt(i) == '1')
+)
  %7B%0A             
@@ -1005,25 +1005,24 @@
 %7D%0A        %7D%0A
-%0A
         for 
@@ -1238,53 +1238,8 @@
   %7D%0A
-        System.out.println(max + %22 %22 + min);%0A
     
@@ -1327,739 +1327,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1432 prob = new Problem1432();%0A//        System.out.println(prob.maxDiff(555)); // 888%0A//        System.out.println(prob.maxDiff(9)); // 8%0A//        System.out.println(prob.maxDiff(123456)); // 820000%0A//        System.out.println(prob.maxDiff(10000)); // 80000%0A//        System.out.println(prob.maxDiff(9288)); // 8700%0A//        System.out.println(prob.maxDiff(9289)); // 8708%0A//        System.out.println(prob.maxDiff(1111)); // 8888%0A//        System.out.println(prob.maxDiff(9999)); // 8888%0A//        System.out.println(prob.maxDiff(1000)); // 8000%0A//        System.out.println(prob.maxDiff(3)); // 8%0A        System.out.println(prob.maxDiff(1101057)); // 8808050%0A    %7D
 %0A%7D%0A

Solve problem 1432