0
0
JavaProgramBeginner · 2 min read

Java Program to Check Palindrome Using Recursion

You can check a palindrome in Java using recursion by creating a method that compares the first and last characters of a string and calls itself with the substring excluding those characters, like boolean isPalindrome(String s, int start, int end).
📋

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome using recursion, compare the first and last characters. If they are the same, call the function again with the substring inside those characters. If at any point characters differ, it's not a palindrome. Stop when the start index is greater or equal to the end index.
📐

Algorithm

1
Get the input string and set start index to 0 and end index to string length minus one.
2
Compare characters at start and end indexes.
3
If characters are different, return false (not a palindrome).
4
If start index is greater or equal to end index, return true (palindrome).
5
Otherwise, call the function recursively with start+1 and end-1.
6
Return the result of the recursive call.
💻

Code

java
public class PalindromeChecker {
    public static 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);
    }

    public static void main(String[] args) {
        String input = "madam";
        if (isPalindrome(input, 0, input.length() - 1)) {
            System.out.println(input + " is a palindrome");
        } else {
            System.out.println(input + " is not a palindrome");
        }
    }
}
Output
madam is a palindrome
🔍

Dry Run

Let's trace the input "madam" through the code

1

Initial call

Check characters at positions 0 ('m') and 4 ('m'), they match.

2

Recursive call 1

Check characters at positions 1 ('a') and 3 ('a'), they match.

3

Recursive call 2

Check characters at positions 2 ('d') and 2 ('d'), start >= end, return true.

4

Return true up the call stack

Each recursive call returns true, final result is true.

CallStart IndexEnd IndexCharacters ComparedResult
104'm' vs 'm'Match, recurse
213'a' vs 'a'Match, recurse
322'd' vs 'd'Start >= End, return true
💡

Why This Works

Step 1: Base Case

When start >= end, it means all characters matched so far, so return true.

Step 2: Mismatch Check

If characters at start and end differ, return false immediately.

Step 3: Recursive Step

If characters match, call the function again with start+1 and end-1 to check inner substring.

🔄

Alternative Approaches

Iterative approach
java
public class PalindromeChecker {
    public static boolean isPalindrome(String s) {
        int start = 0, end = s.length() - 1;
        while (start < end) {
            if (s.charAt(start) != s.charAt(end)) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }

    public static void main(String[] args) {
        String input = "madam";
        System.out.println(input + (isPalindrome(input) ? " is a palindrome" : " is not a palindrome"));
    }
}
Uses a loop instead of recursion; simpler for beginners but no recursive practice.
Using String reversal
java
public class PalindromeChecker {
    public static boolean isPalindrome(String s) {
        String reversed = new StringBuilder(s).reverse().toString();
        return s.equals(reversed);
    }

    public static void main(String[] args) {
        String input = "madam";
        System.out.println(input + (isPalindrome(input) ? " is a palindrome" : " is not a palindrome"));
    }
}
Simple and fast but does not use recursion.

Complexity: O(n) time, O(n) space

Time Complexity

The function compares pairs of characters moving inward, so it runs in linear time proportional to the string length.

Space Complexity

Each recursive call adds a new frame to the call stack, so space is linear in the worst case.

Which Approach is Fastest?

The iterative approach uses constant space and is faster in practice, but recursion is good for learning and clarity.

ApproachTimeSpaceBest For
RecursiveO(n)O(n)Learning recursion, clarity
IterativeO(n)O(1)Performance and memory efficiency
String reversalO(n)O(n)Simplicity, no recursion
💡
Always check the base case first in recursion to avoid infinite calls.
⚠️
Forgetting to move the start and end indexes closer in the recursive call causes infinite recursion.