Last Updated : 23 Jul, 2025
Given a string s, the task is to check if it is a palindrome or not.
Examples:
Using Recursion and Two Pointers - O(n) time and O(n) spaceInput: s = "abba"
Output: Yes
Explanation: s is a palindromeInput: s = "abc"
Output: No
Explanation: s is not a palindrome
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:
// 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");
}
Time Complexity: O(n)
Auxiliary Space: O(n) due to call stack.
C++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.
// 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");
}
Related Article:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4