A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string )
LANGUAGE: Java
CHALLENGE:
A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string ). Write a recursive, boolean -valued method, isPalindrome that accepts a string and returns whether the string is a palindrome.
A string , s, is a palindrome if:
• s is the empty string or s consists of a single letter (which reads the same back or forward), or
• the first and last characters of s are the same, and the rest of the string (i.e., the second through next-to-last characters ) form a palindrome.
SOLUTION:
public static boolean isPalindrome(String s) { if(s.length() == 0 || s.length() == 1) return true; if(s.charAt(0) == s.charAt(s.length()-1)) return isPalindrome(s.substring(1, s.length()-1)); return false; }