Skip to content
thesarfo

Reference

Checking for a Palindrome Recursively

Comparing the first and last characters of a string and recursing inward until the middle is reached.

views 0

A palindrome reads the same forward and backward. We compare the first and last characters; if they match, we move inward and repeat.

boolean isPalindrome(String s, int start, int end) {
if (start >= end) return true;
if (s.charAt(start) != s.charAt(end)) return false;
return isPalindrome(s, start + 1, end - 1);
}

When all corresponding pairs match until the middle is reached, the string is a palindrome.