Given a string S, the task is to find the length longest sub-string which is a palindrome
Examples:
Input: S = “aaaabbaa”
Output: 6
Explanation:
Sub-string “aabbaa” is the longest palindromic sub-string.
Input: S = “banana”
Output: 5
Explanation:
Sub-string “anana” is the longest palindromic sub-string.
Approach:The idea is to use recursion to break the problem into smaller sub-problems. In order to break the problem into two smaller sub-problems, Compare the start and end characters of the string and recursively call the function for the middle substring. Below is the illustration of the recursion:
- Base Case: The base case for this problem is when the starting index of the string is greater than or equal to the ending index.
if (start > end)
return count
if (start == end)
return count + 1
- Recursive Case: Compare the characters at the start and end index of the string:
- When starting and ending characters are equal, then recursively call for the substring by excluding the starting and ending characters
recursive_func(string, start+1, end-1)
- When starting and ending characters are not equal, then recursively call for the substring by excluding the starting and ending characters one at a time.
recursive_func(string, start+1, end)
recursive_func(string, start, end-1)
- Return statement: At each recursive call, return the maximum count possible by including and excluding the start and end characters.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
int max( int x, int y)
{
return (x > y) ? x : y;
}
int longestPalindromic(string str,
int i, int j, int count)
{
if (i > j)
return count;
if (i == j)
return (count + 1);
if (str[i] == str[j]) {
count = longestPalindromic(str, i + 1,
j - 1, count + 2);
return max(count,
max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
return max(
longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
int longest_palindromic_substr(string str)
{
return longestPalindromic(str, 0,
str.length() - 1, 0);
}
int main()
{
string str = "aaaabbaa" ;
cout << longest_palindromic_substr(str);
return 0;
}
|
Java
class GFG{
static int max( int x, int y)
{
return (x > y) ? x : y;
}
static int longestPalindromic(String str,
int i, int j, int count)
{
if (i > j)
return count;
if (i == j)
return (count + 1 );
if (str.charAt(i) == str.charAt(j)) {
count = longestPalindromic(str, i + 1 ,
j - 1 , count + 2 );
return max(count,
max(longestPalindromic(str, i + 1 , j, 0 ),
longestPalindromic(str, i, j - 1 , 0 )));
}
return Math.max(
longestPalindromic(str, i + 1 , j, 0 ),
longestPalindromic(str, i, j - 1 , 0 ));
}
static int longest_palindromic_substr(String str)
{
return longestPalindromic(str, 0 ,
str.length() - 1 , 0 );
}
public static void main(String[] args)
{
String str = "aaaabbaa" ;
System.out.print(longest_palindromic_substr(str));
}
}
|
Python3
def maxi(x, y) :
if x > y :
return x
else :
return y
def longestPalindromic(strn, i, j, count):
if i > j :
return count
if i = = j :
return (count + 1 )
if strn[i] = = strn[j] :
count = longestPalindromic(strn, i + 1 , j - 1 , count + 2 )
return maxi(count, maxi(longestPalindromic(strn, i + 1 , j, 0 ),
longestPalindromic(strn, i, j - 1 , 0 )))
return maxi( longestPalindromic(strn, i + 1 , j, 0 ),
longestPalindromic(strn, i, j - 1 , 0 ))
def longest_palindromic_substr(strn):
k = len (strn) - 1
return longestPalindromic(strn, 0 , k, 0 )
strn = "aaaabbaa"
print ( longest_palindromic_substr(strn) )
|
C#
using System;
class GFG{
static int max( int x, int y)
{
return (x > y) ? x : y;
}
static int longestPalindromic(String str,
int i, int j, int count)
{
if (i > j)
return count;
if (i == j)
return (count + 1);
if (str[i] == str[j]) {
count = longestPalindromic(str, i + 1,
j - 1, count + 2);
return max(count,
max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
return Math.Max(
longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
static int longest_palindromic_substr(String str)
{
return longestPalindromic(str, 0,
str.Length - 1, 0);
}
public static void Main(String[] args)
{
String str = "aaaabbaa" ;
Console.Write(longest_palindromic_substr(str));
}
}
|
Javascript
<script>
function max(x, y)
{
return (x > y) ? x : y;
}
function longestPalindromic(str, i, j, count)
{
if (i > j)
return count;
if (i == j)
return (count + 1);
if (str[i] == str[j]) {
count = longestPalindromic(str, i + 1,
j - 1, count + 2);
return max(count,
max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
return Math.max(
longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
function longest_palindromic_substr(str)
{
return longestPalindromic(str, 0, str.length - 1, 0);
}
let str = "aaaabbaa" ;
document.write(longest_palindromic_substr(str));
</script>
|
Time Complexity : The time complexity of this code is O(n^2) as there are two recursive calls in each iteration and the loop iterates over all characters of the string.
Auxiliary Space : The space complexity is O(n) as the recursive function calls consume space on the call stack.