public class Palindrome3 { public static void main(String[] args) { String s = "racecar"; int lastPos = s.length() - 1; boolean ans; ans = isPalindrome(s, 0, lastPos); } public static boolean isPalindrome(String w, int startPos, int endPos) { if ( startPos >= endPos ) { // base cases return true; } else { boolean helpSol = isPalindrome(w, startPos+1, endPos-1); return (w.charAt(startPos) == w.charAt(endPos)) && helpSol; } } }