Given a string, write a recursive function that checks if the given string is a palindrome, else, not a palindrome.
Examples:
Input : malayalam
Output : Yes
Reverse of malayalam is also
malayalam.
Input : max
Output : No
Reverse of max is not max.
We have discussed an iterative function here.
The idea of a recursive function is simple:
1) If there is only one character in string
return true.
2) Else compare first and last characters
and recur for remaining substring.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalRec(char str[],
int s, int e)
{
if (s == e)
return true;
if (str[s] != str[e])
return false;
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
bool isPalindrome(char str[])
{
int n = strlen(str);
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
int main()
{
char str[] = "geeg";
if (isPalindrome(str))
cout << "Yes";
else
cout << "No";
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isPalRec(char str[],
int s, int e)
{
if (s == e)
return true;
if (str[s] != str[e])
return false;
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
bool isPalindrome(char str[])
{
int n = strlen(str);
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
int main()
{
char str[] = "geeg";
if (isPalindrome(str))
printf("Yes");
else
printf("No");
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isPalRec(String str,
int s, int e)
{
if (s == e)
return true;
if ((str.charAt(s)) != (str.charAt(e)))
return false;
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
static boolean isPalindrome(String str)
{
int n = str.length();
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
public static void main(String args[])
{
String str = "geeg";
if (isPalindrome(str))
System.out.println("Yes");
else
System.out.println("No");
}
}
|
Python
def isPalRec(st, s, e) :
if (s == e):
return True
if (st[s] != st[e]) :
return False
if (s < e + 1) :
return isPalRec(st, s + 1, e - 1);
return True
def isPalindrome(st) :
n = len(st)
if (n == 0) :
return True
return isPalRec(st, 0, n - 1);
st = "geeg"
if (isPalindrome(st)) :
print "Yes"
else :
print "No"
|
C#
using System;
class GFG
{
static bool isPalRec(String str,
int s,
int e)
{
if (s == e)
return true;
if ((str[s]) != (str[e]))
return false;
if (s < e + 1)
return isPalRec(str, s + 1,
e - 1);
return true;
}
static bool isPalindrome(String str)
{
int n = str.Length;
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
public static void Main()
{
String str = "geeg";
if (isPalindrome(str))
Console.Write("Yes");
else
Console.Write("No");
}
}
|
PHP
<?php
function isPalRec($str, $s,$e)
{
if ($s == $e)
return true;
if ($str[$s] != $str[$e])
return false;
if ($s < $e + 1)
return isPalRec($str, $s + 1, $e - 1);
return true;
}
function isPalindrome($str)
{
$n = strlen($str);
if ($n == 0)
return true;
return isPalRec($str, 0, $n - 1);
}
{
$str = "geeg";
if (isPalindrome($str))
echo("Yes");
else
echo("No");
return 0;
}
?>
|
Javascript
<script>
function isPalRec( str , s , e) {
if (s == e)
return true;
if ((str.charAt(s)) != (str.charAt(e)))
return false;
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
function isPalindrome( str) {
var n = str.length;
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
var str = "geeg";
if (isPalindrome(str))
document.write("Yes");
else
document.write("No");
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Another Approach :
Basically while traversing check whether ith and n-i-1th index are equal or not.
If there are not equal return false and if they are equal then continue with the recursion calls.
C++
#include <iostream>
using namespace std;
bool isPalindrome(string s, int i){
if(i > s.size()/2){
return true ;
}
return s[i] == s[s.size()-i-1] && isPalindrome(s, i+1) ;
}
int main()
{
string str = "geeg" ;
if (isPalindrome(str, 0))
cout << "Yes";
else
cout << "No";
return 0;
}
|
Java
import java.io.*;
class GFG {
public static boolean isPalindrome(String s, int i){
if(i > s.length()/2)
{
return true ;
}
return s.charAt(i) == s.charAt(s.length()-i-1) && isPalindrome(s, i+1) ;
}
public static void main (String[] args) {
String str = "geeg" ;
if (isPalindrome(str, 0))
{ System.out.println("Yes"); }
else
{ System.out.println("No"); }
}
}
|
Python3
def isPalindrome(s, i):
if(i > len(s)/2):
return True
ans = False
if((s[i] is s[len(s) - i - 1]) and isPalindrome(s, i + 1)):
ans = True
return ans
str = "geeg"
if (isPalindrome(str, 0)):
print("Yes")
else:
print("No")
|
C#
using System;
public class GFG{
public static bool isPalindrome(string s, int i){
if(i > s.Length/2){
return true ;
}
return s[i] == s[s.Length-i-1] && isPalindrome(s, i+1) ;
}
public static void Main (){
string str = "geeg" ;
if (isPalindrome(str, 0))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
|
Javascript
<script>
function isPalindrome(s,i){
if(i > s.length/2)
{return true;}
return s[i] == s[s.length-i-1] && isPalindrome(s, i+1)
}
let str = "geeg";
let ans = isPalindrome(str, 0);
if (ans == true)
{
console.log("Yes");}
else
{
console.log("No");}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!