public String longestPalindrome(String s){ if(s == null || s.length() == 0) return""; int maxLen = 0; int res_left = 0; int res_right = 0; for(int i = 0; i < 2 * s.length() - 1; i++){ int left = i / 2; int right = i / 2 + i % 2; while(left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){ left--; right++; } int len = right - left - 1; if(len > maxLen){ maxLen = len; res_left = left + 1; res_right = right - 1; } } return s.substring(res_left, res_right + 1); }