package com.fishercoder.solutions;

/**
 * 564. Find the Closest Palindrome
 *
 * Given an integer n, find the closest integer (not including itself), which is a palindrome.

 The 'closest' is defined as absolute difference minimized between two integers.

 Example 1:

 Input: "123"
 Output: "121"

 Note:

 The input n is a positive integer represented by string, whose length will not exceed 18.
 If there is a tie, return the smaller one as answer.

 */
public class _564 {
    public static class Solution1 {

        public String nearestPalindromic(String n) {
            if (n.length() >= 2 && allNine(n)) {
                String s = "1";
                for (int i = 0; i < n.length() - 1; i++) {
                    s += "0";
                }
                s += "1";
                return s;
            }
            boolean isOdd = (n.length() % 2 != 0);
            String left = n.substring(0, (n.length() + 1) / 2);
            long[] increment = {-1, 0, +1};
            String ret = n;
            long minDiff = Long.MAX_VALUE;
            for (long i : increment) {
                String s = getPalindrom(Long.toString(Long.parseLong(left) + i), isOdd);
                if (n.length() >= 2 && (s.length() != n.length() || Long.parseLong(s) == 0)) {
                    s = "";
                    for (int j = 0; j < n.length() - 1; j++) {
                        s += "9";
                    }
                }
                long diff = s.equals(n) ? Long.MAX_VALUE : Math.abs(Long.parseLong(s) - Long.parseLong(n));
                if (diff < minDiff) {
                    minDiff = diff;
                    ret = s;
                }
            }
            return ret;
        }

        private String getPalindrom(String s, boolean isOdd) {
            String right = new StringBuilder(s).reverse().toString();
            return isOdd ? s.substring(0, s.length() - 1) + right : s + right;
        }

        private boolean allNine(String s) {
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) != '9') {
                    return false;
                }
            }
            return true;
        }
    }
}

@@ -33,430 +33,8 @@
 s;%0A%0A
-/**%0A * 564. Find the Closest Palindrome%0A *%0A * Given an integer n, find the closest integer (not including itself), which is a palindrome.%0A%0A The 'closest' is defined as absolute difference minimized between two integers.%0A%0A Example 1:%0A%0A Input: %22123%22%0A Output: %22121%22%0A%0A Note:%0A%0A The input n is a positive integer represented by string, whose length will not exceed 18.%0A If there is a tie, return the smaller one as answer.%0A%0A */%0A
 publ

refactor 564