Open In App

Check for Palindrome Using Recursion

Last Updated : 24 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string s, check if it is a palindrome using recursion. A palindrome is a word, phrase, or sequence that reads the same backward as forward.

Examples:

Input: s = "abba"
Output: true
Explanation: The first and last characters match, and the middle substring "bb" is also a palindrome, so the whole string is a palindrome.

Input: s = "abc"
Output: false
Explanation: The first and last characters don’t match (a ≠ c), so the string is not a palindrome.

[Approach] Using Recursion and Two Pointers - O(n) time and O(n) space

The idea is to recursively check if the string is palindrome or not. Initialize two pointers: one to point to starting index and one to point to ending index. Compare the characters at starting and ending indices. If the characters match, recursively check for inner substring. Otherwise, return false

C++
#include <iostream>
#include <string>
using namespace std;

// Helper recursive function to check palindrome
bool isPalindromeRec(string& s, int left, int right) {
    
    // Base case
    if (left >= right) 
        return true;
    
    // If mismatch found
    if (s[left] != s[right]) 
        return false;

    // Recursive call with narrowed range
    return isPalindromeRec(s, left + 1, right - 1);
}

bool isPalindrome(string& s) {
    return isPalindromeRec(s, 0, s.size() - 1);
}

int main() {
    string s = "abba";
    
    if (isPalindrome(s)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }

    return 0;
}
Java
class GFG {
    
    // Helper recursive function
    static boolean isPalindromeRec(String s, int left, int right) {
        
        // Base case
        if (left >= right)
            return true;
        
        // If mismatch found
        if (s.charAt(left) != s.charAt(right))
            return false;
        
        // Recursive call with narrowed range
        return isPalindromeRec(s, left + 1, right - 1);
    }
    
    static boolean isPalindrome(String s) {
        return isPalindromeRec(s, 0, s.length() - 1);
    }
    
    public static void main(String[] args) {
        String s = "abba";
        
        if (isPalindrome(s))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
# Helper recursive function
def isPalindromeRec(s, left, right):
        
    # Base case
    if left >= right:
        return True
        
    # If mismatch found
    if s[left] != s[right]:
        return False
        
    # Recursive call with narrowed range
    return isPalindromeRec(s, left + 1, right - 1)
    
def isPalindrome(s):
    return isPalindromeRec(s, 0, len(s) - 1)


if __name__ == "__main__":
    s = "abba"
    
    if isPalindrome(s):
        print("true")
    else:
        print("false")
C#
using System;

class GFG {
    
    // Helper recursive function
    static bool isPalindromeRec(string s, int left, int right) {
        
        // Base case
        if (left >= right)
            return true;
        
        // If mismatch found
        if (s[left] != s[right])
            return false;
        
        // Recursive call with narrowed range
        return isPalindromeRec(s, left + 1, right - 1);
    }
    
    static bool isPalindrome(string s) {
        return isPalindromeRec(s, 0, s.Length - 1);
    }
    
    static void Main() {
        string s = "abba";
        
        if (isPalindrome(s))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// Helper recursive function
function isPalindromeRec(s, left, right) {
        
    // Base case
    if (left >= right)
        return true;
        
    // If mismatch found
    if (s[left] !== s[right])
        return false;
        
    // Recursive call with narrowed range
    return isPalindromeRec(s, left + 1, right - 1);
}
    
function isPalindrome(s) {
    return isPalindromeRec(s, 0, s.length - 1);
}

// Driver Code
let s = "abba";

if (isPalindrome(s))
    console.log("true");
else
    console.log("false");

Output
true

Related Article:


Article Tags :

Explore