public class Palindrome4 { public static void main(String[] args) { String s = "racecar"; boolean ans; ans = isPalindrome(s); } public static boolean isPalindrome(String w) { int lastPos = w.length() - 1; return isPalindrome(w, 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; } } }