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 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;
    }

}

@@ -471,17 +471,57 @@
  _564 %7B%0A
-%0A
+    public static class Solution1 %7B%0A%0A    
     publ
@@ -561,32 +561,36 @@
 ng n) %7B%0A        
+    
 if (n.length() %3E
@@ -614,32 +614,36 @@
 ) %7B%0A            
+   
+ 
 String s = %221%22;%0A
@@ -638,24 +638,28 @@
 ng s = %221%22;%0A
+    
             
@@ -713,24 +713,28 @@
             
+ 
+   
 s += %220%22;%0A  
@@ -731,16 +731,20 @@
 += %220%22;%0A
+    
         
@@ -757,24 +757,28 @@
             
+    
 s += %221%22;%0A  
@@ -783,24 +783,28 @@
             
+  
+  
 return s;%0A  
@@ -801,34 +801,42 @@
 turn s;%0A        
-%7D%0A
+    %7D%0A    
         boolean 
@@ -870,24 +870,28 @@
 0);%0A        
+   
+ 
 String left 
@@ -934,24 +934,28 @@
 2);%0A        
+    
 long%5B%5D incre
@@ -974,32 +974,36 @@
 0, +1%7D;%0A        
+  
+  
 String ret = n;%0A
@@ -1002,16 +1002,20 @@
 et = n;%0A
+    
         
@@ -1049,24 +1049,28 @@
 UE;%0A        
+    
 for (long i 
@@ -1088,32 +1088,36 @@
 ) %7B%0A            
+   
+ 
 String s = getPa
@@ -1173,16 +1173,20 @@
 isOdd);%0A
+    
         
@@ -1288,16 +1288,20 @@
     
+ 
+   
 s = %22%22;%0A
     
@@ -1296,16 +1296,20 @@
 s = %22%22;%0A
+    
         
@@ -1375,24 +1375,28 @@
             
+    
 s += %229%22;%0A  
@@ -1401,34 +1401,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
             %7D%0A  
@@ -1425,32 +1425,36 @@
               %7D%0A
+    
             long
@@ -1549,24 +1549,28 @@
             
+   
+ 
 if (diff %3C m
@@ -1575,24 +1575,28 @@
  minDiff) %7B%0A
+    
             
@@ -1631,16 +1631,20 @@
         
+    
 ret = s;
@@ -1640,24 +1640,28 @@
    ret = s;%0A
+    
             
@@ -1662,34 +1662,42 @@
       %7D%0A        
-%7D%0A
+    %7D%0A    
         return r
@@ -1700,16 +1700,20 @@
 rn ret;%0A
+    
     %7D%0A%0A 
@@ -1707,32 +1707,36 @@
 %0A        %7D%0A%0A    
+ 
+   
 private String g
@@ -1770,24 +1770,28 @@
 an isOdd) %7B%0A
+    
         Stri
@@ -1848,24 +1848,28 @@
 ();%0A        
+    
 return isOdd
@@ -1923,27 +1923,35 @@
  right;%0A    
+  
+  
 %7D%0A%0A
+    
     private 
@@ -1978,32 +1978,36 @@
 ng s) %7B%0A        
+ 
+   
 for (int i = 0; 
@@ -2037,24 +2037,28 @@
             
+   
+ 
 if (s.charAt
@@ -2067,24 +2067,28 @@
 ) != '9') %7B%0A
+    
             
@@ -2109,34 +2109,42 @@
 se;%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -2141,24 +2141,28 @@
   %7D%0A        
+    
 return true;
@@ -2166,13 +2166,22 @@
 ue;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 564