public String longestPalindrome(String s) { if (s.length() == 0 || s.length() == 1) return s; char[] chars = s.toCharArray(); StringmaxString= String.valueOf(chars[0]); for (inti=0; i < chars.length; i++) { intn=0; intlen=1; while (i - n - 1 >= 0 && i + n + 1 < chars.length && chars[i - n - 1] == chars[i + n + 1]) { n++; len += 2; } if (len > maxString.length()) { maxString = s.substring(i - n, i + n + 1); } if (i + 1 < chars.length && chars[i] == chars[i + 1]) { n = 0; len = 2; while (i - n - 1 >= 0 && i + 1 + n + 1 < chars.length && chars[i - n - 1] == chars[i + 1 + n + 1]) { n++; len += 2; } if (len > maxString.length()) { maxString = s.substring(i - n, i + 1 + n + 1); } } } return maxString; }