Given a string S, the task is to find the length of the longest substring which is a palindrome.
Examples:
Input: S = "aaaabbaa"
Output: 6
Explanation: Substring "aabbaa" is the longest palindromic sub-string.Input: S = "banana"
Output: 5
Explanation: Substring "anana" is the longest palindromic sub-string.
[Approach] Using Recursion - O(2^n) Time and O(n) Space
The idea is to use recursion to break the problem into smaller sub-problems. To break the problem into two smaller sub-problems, compare the start and end characters of the string until the starting index becomes greater than the ending index.
- If the starting and ending characters are equal, then recursively call for the substring by excluding the starting and ending characters.
- If starting and ending characters are not equal, then recursively call for the substring by excluding the starting and ending characters one at a time.
//Driver Code Starts
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//Driver Code Ends
int longestPalindromic(string str, int i, int j, int count)
{
// Base case: invalid substring
if (i > j)
return count;
// Base case: single character is palindrome of length 1
if (i == j)
return count + 1;
// If characters match, extend palindrome
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)));
}
// If characters don't match, explore both possibilities
return max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
//Driver Code Starts
int main()
{
string str = "aaaabbaa";
int n = str.length();
cout << longestPalindromic(str, 0, n - 1, 0);
return 0;
}
//Driver Code Ends
//Driver Code Starts
import java.util.*;
//Driver Code Ends
public class GfG {
static int longestPalindromic(String str, int i, int j, int count)
{
// Base case: invalid substring
if (i > j)
return count;
// Base case: single character is palindrome of length 1
if (i == j)
return count + 1;
// If characters match, extend palindrome
if (str.charAt(i) == str.charAt(j)) {
count = longestPalindromic(str, i + 1, j - 1, count + 2);
return Math.max(count,
Math.max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
// If characters don't match, explore both possibilities
return Math.max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
//Driver Code Starts
public static void main(String[] args)
{
String str = "aaaabbaa";
int n = str.length();
System.out.println(longestPalindromic(str, 0, n - 1, 0));
}
}
//Driver Code Ends
def longestPalindromic(str, i, j, count):
# Base case: invalid substring
if i > j:
return count
# Base case: single character is palindrome of length 1
if i == j:
return count + 1
# If characters match, extend palindrome
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)))
# If characters don't match, explore both possibilities
return max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0))
if __name__ == "__main__":
str = "aaaabbaa"
n = len(str)
print(longestPalindromic(str, 0, n - 1, 0))
//Driver Code Starts
using System;
//Driver Code Ends
class GfG
{
static int longestPalindromic(string str, int i, int j, int count)
{
// Base case: invalid substring
if (i > j)
return count;
// Base case: single character is palindrome of length 1
if (i == j)
return count + 1;
// If characters match, extend palindrome
if (str[i] == str[j])
{
count = longestPalindromic(str, i + 1, j - 1, count + 2);
return Math.Max(count,
Math.Max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
// If characters don't match, explore both possibilities
return Math.Max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
//Driver Code Starts
static void Main()
{
string str = "aaaabbaa";
int n = str.Length;
Console.WriteLine(longestPalindromic(str, 0, n - 1, 0));
}
}
//Driver Code Ends
function longestPalindromic(str, i, j, count)
{
// Base case: invalid substring
if (i > j)
return count;
// Base case: single character is palindrome of length 1
if (i == j)
return count + 1;
// If characters match, extend palindrome
if (str[i] === str[j]) {
count = longestPalindromic(str, i + 1, j - 1, count + 2);
return Math.max(count,
Math.max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
// If characters don't match, explore both possibilities
return Math.max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
let str = "aaaabbaa";
let n = str.length;
console.log(longestPalindromic(str, 0, n - 1, 0));
Output
6
Please note that the above solution can be optimized. Please refer Longest Palindromic Substring for more details.