Open In App

Recursive function to check if a string is palindrome

Last Updated : 11 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string s, the task is to check if it is a palindrome or not.

Examples:

Input: s = "abba"
Output: Yes
Explanation: s is a palindrome

Input: s = "abc"
Output: No
Explanation: s is not a palindrome

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.

Step by step approach:

  1. Initialize two pointers: left and right, to point to the starting and ending indices.
  2. Recursively, compare the characters present at left and right index. Base case will be true if left is greater than equal to right.
  3. If the characters don't match, return false.
  4. Otherwise, recursively compare the inner substring, i.e., left + 1 and right - 1.
C++
// Java program to check if a string is palindrome
#include <bits/stdc++.h>
using namespace std;

// Recursive util function to check if a string is a palindrome
bool isPalindromeUtil(string& s, int left, int right) {

    // Base case
    if (left >= right) 
        return true;
    
    // If the characters at the current positions 
    // are not equal, it is not a palindrome
    if (s[left] != s[right]) 
        return false;

    // Move left pointer to the right
    // and right pointer to the left
    return isPalindromeUtil(s, left + 1, right - 1);
}

// Function to check if a string is a palindrome
bool isPalindrome(string s){
    int left = 0, right = s.length() - 1;
    return isPalindromeUtil(s, left, right);
}

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

    return 0;
}
Java
// C++ program to check if a string is palindrome

class GfG {
    
    // Recursive util function to check if a string is a palindrome
    static boolean isPalindromeUtil(String s, int left, int right) {

        // Base case
        if (left >= right) 
            return true;
        
        // If the characters at the current positions 
        // are not equal, it is not a palindrome
        if (s.charAt(left) != s.charAt(right)) 
            return false;

        // Move left pointer to the right
        // and right pointer to the left
        return isPalindromeUtil(s, left + 1, right - 1);
    }

    // Function to check if a string is a palindrome
    static boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        return isPalindromeUtil(s, left, right);
    }

    public static void main(String[] args) {
        String s = "abba";
        if (isPalindrome(s)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
Python
# Python program to check if a string is palindrome

# Recursive util function to check if a string is a palindrome
def isPalindromeUtil(s, left, right):

    # Base case
    if left >= right:
        return True
    
    # If the characters at the current positions 
    # are not equal, it is not a palindrome
    if s[left] != s[right]:
        return False

    # Move left pointer to the right
    # and right pointer to the left
    return isPalindromeUtil(s, left + 1, right - 1)

# Function to check if a string is a palindrome
def isPalindrome(s):
    left = 0
    right = len(s) - 1
    return isPalindromeUtil(s, left, right)

if __name__ == "__main__":
    s = "abba"
    if isPalindrome(s):
        print("Yes")
    else:
        print("No")
C#
// C# program to check if a string is palindrome

using System;

class GfG {
    
    // Recursive util function to check if a string is a palindrome
    static bool isPalindromeUtil(string s, int left, int right) {

        // Base case
        if (left >= right) 
            return true;
        
        // If the characters at the current positions 
        // are not equal, it is not a palindrome
        if (s[left] != s[right]) 
            return false;

        // Move left pointer to the right
        // and right pointer to the left
        return isPalindromeUtil(s, left + 1, right - 1);
    }

    // Function to check if a string is a palindrome
    static bool isPalindrome(string s) {
        int left = 0, right = s.Length - 1;
        return isPalindromeUtil(s, left, right);
    }

    static void Main() {
        string s = "abba";
        if (isPalindrome(s)) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// C++ program to check if a string is palindrome

// Recursive util function to check if a string is a palindrome
function isPalindromeUtil(s, left, right) {

    // Base case
    if (left >= right) 
        return true;
    
    // If the characters at the current positions 
    // are not equal, it is not a palindrome
    if (s[left] !== s[right]) 
        return false;

    // Move left pointer to the right
    // and right pointer to the left
    return isPalindromeUtil(s, left + 1, right - 1);
}

// Function to check if a string is a palindrome
function isPalindrome(s) {
    let left = 0, right = s.length - 1;
    return isPalindromeUtil(s, left, right);
}

let s = "abba";
if (isPalindrome(s)) {
    console.log("Yes");
}
else {
    console.log("No");
}

Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(n) due to call stack.

Using Recursion and 1 Pointer - O(n) time and O(n) space

The idea is similar to first approach, but instead of using 2 pointers, we will use only starting index pointer, and use this and length of string to find the ending index pointer.

C++
// Java program to check if a string is palindrome
#include <bits/stdc++.h>
using namespace std;

// Recursive util function to check if a string is a palindrome
bool isPalindromeUtil(string& s, int left) {
    int right = s.length() - 1 - left;

    // Base case
    if (left >= right) 
        return true;
    
    // If the characters at the current positions 
    // are not equal, it is not a palindrome
    if (s[left] != s[right]) 
        return false;

    // Move left pointer to the right
    // and right pointer to the left
    return isPalindromeUtil(s, left + 1);
}

// Function to check if a string is a palindrome
bool isPalindrome(string s){
    int left = 0;
    return isPalindromeUtil(s, left);
}

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

    return 0;
}
Java
// Java program to check if a string is palindrome

class GfG {
    
    // Recursive util function to check if a string is a palindrome
    static boolean isPalindromeUtil(String s, int left) {
        int right = s.length() - 1 - left;

        // Base case
        if (left >= right) 
            return true;
        
        // If the characters at the current positions 
        // are not equal, it is not a palindrome
        if (s.charAt(left) != s.charAt(right)) 
            return false;

        // Move left pointer to the right
        // and right pointer to the left
        return isPalindromeUtil(s, left + 1);
    }

    // Function to check if a string is a palindrome
    static boolean isPalindrome(String s) {
        int left = 0;
        return isPalindromeUtil(s, left);
    }

    public static void main(String[] args) {
        String s = "abba";
        if (isPalindrome(s)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
Python
# Python program to check if a string is palindrome

# Recursive util function to check if a string is a palindrome
def isPalindromeUtil(s, left):
    right = len(s) - 1 - left

    # Base case
    if left >= right:
        return True
    
    # If the characters at the current positions 
    # are not equal, it is not a palindrome
    if s[left] != s[right]:
        return False

    # Move left pointer to the right
    # and right pointer to the left
    return isPalindromeUtil(s, left + 1)

# Function to check if a string is a palindrome
def isPalindrome(s):
    left = 0
    return isPalindromeUtil(s, left)

if __name__ == "__main__":
    s = "abba"
    if isPalindrome(s):
        print("Yes")
    else:
        print("No")
C#
// C# program to check if a string is palindrome

using System;

class GfG {
    
    // Recursive util function to check if a string is a palindrome
    static bool isPalindromeUtil(string s, int left) {
        int right = s.Length - 1 - left;

        // Base case
        if (left >= right) 
            return true;
        
        // If the characters at the current positions 
        // are not equal, it is not a palindrome
        if (s[left] != s[right]) 
            return false;

        // Move left pointer to the right
        // and right pointer to the left
        return isPalindromeUtil(s, left + 1);
    }

    // Function to check if a string is a palindrome
    static bool isPalindrome(string s) {
        int left = 0;
        return isPalindromeUtil(s, left);
    }

    static void Main() {
        string s = "abba";
        if (isPalindrome(s)) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to check if a string is palindrome

// Recursive util function to check if a string is a palindrome
function isPalindromeUtil(s, left) {
    let right = s.length - 1 - left;

    // Base case
    if (left >= right) 
        return true;
    
    // If the characters at the current positions 
    // are not equal, it is not a palindrome
    if (s[left] !== s[right]) 
        return false;

    // Move left pointer to the right
    // and right pointer to the left
    return isPalindromeUtil(s, left + 1);
}

// Function to check if a string is a palindrome
function isPalindrome(s) {
    let left = 0;
    return isPalindromeUtil(s, left);
}

let s = "abba";
if (isPalindrome(s)) {
    console.log("Yes");
}
else {
    console.log("No");
}

Output
Yes

Related Article:


Next Article
Article Tags :
Practice Tags :

Similar Reads