# Find The Closest Palindrome

• Can anyone explain line 24 and 25:

``````            mid = (len(s) - 1) // 2
s[mid] = '9'
``````

Thank you!

• This is easier to understand -
class Solution {

``````public String nearestPalindromic(String n) {
int order = (int) Math.pow(10, n.length()/2);
Long ans = Long.valueOf(new String(n));
Long noChange = mirror(ans);
Long larger = mirror((ans/order)*order + order+1);
Long smaller = mirror((ans/order)*order - 1 );
if ( noChange > ans) {
larger = (long) Math.min(noChange, larger);
} else if ( noChange < ans) {
smaller = (long) Math.max(noChange, smaller);
}
return String.valueOf( ans - smaller <= larger - ans ? smaller :larger) ;
}
Long mirror(Long ans) {
char[] a = String.valueOf(ans).toCharArray();
int i = 0;
int j = a.length-1;
while (i < j) {
a[j--] = a[i++];
}
return Long.valueOf(new String(a));
}
``````

}

• Python makes this look like a joke:

def closestPalindrome(x: str) -> str:
return x[:(len(x) + 1) // 2] + "".join(reversed(x[:len(x) // 2]))

• Even better:
def closestPalindrome(x):
return x[:(len(x) + 1) // 2] + x[len(x) // 2 - 1:: -1]

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.