Deves retirar o primeiro e o último caracter. A palavra é palíndromo se os caracteres forem iguais e a String resultante for também um palíndromo.
public static boolean isPalindrome(String s){
if (s.length() <2){
return true; // String vazia ou com um caracter é palíndromo
}
char first = s.charAt(0);
char last = s.charAt(s.length()-1);
return first == last && isPalindrome(s.substring(1, s.length()-1));
}
public static void main( String[] args ) {
String[] ss = {
"",
"a",
"aa",
"ab",
"arara",
"arbra",
"palíndromo"
};
for ( String s : ss ) {
System.out.printf(
"\"%s\" é palíndromo? %s\n",
s,
isPalindrome( s ) ? "sim" : "não" );
}
}
public static boolean isPalindrome( String s ) {
return isPalindrome( s, 0, s.length() - 1 );
}
private static boolean isPalindrome( String s, int i, int f ) {
if ( i >= f ) {
return true;
} else {
if ( s.charAt( i ) == s.charAt( f ) ) {
return isPalindrome( s, i+1, f-1 );
} else {
return false;
}
}
}
Há duas bases para a recursão:
quando os caracteres das extremidades forem diferentes;
quando acabarem os caracteres que precisam ser verificados, pois há um caractere central (i = f) ou os caracteres acabaram (i > f).
O passo indutivo/recursivo é quando os dois caracteres são iguais, então move-se as posições para mais para dentro da String, invocando novamente método.