class Solution { public String longestPalindrome(String s) { return getString(s, 0, s.length()-1); } public String getString(String s, int left, int right){ int rememberLeft = left; int rememberRight = right; while(s.charAt(left) == s.charAt(right)){ if(left >= right){ return s.substring(rememberLeft, rememberRight+1); } left++; right--; } String s1 = getString(s, rememberLeft+1, rememberRight); String s2 = getString(s, rememberLeft, rememberRight-1); return (s1.length() > s2.length()) ? s1 : s2; } } class Test{ public static void main(String[] args) { Solution s = new Solution(); System.out.println(s.longestPalindrome("babaddtattarra")); } }