Maximum length palindromic substring for every index such that it starts and ends at that index
Last Updated :
07 May, 2021
Given a string S, the task for every index of the string is to find the length of the longest palindromic substring that either starts or ends at that index.
Examples:
Input: S = “bababa”
Output: 5 5 3 3 5 5
Explanation:
Longest palindromic substring starting at index 0 is “babab”. Therefore, length = 5
Longest palindromic substring starting at index 1 is “ababa”. Therefore, length = 5
Longest palindromic substring ending at index 2 is “bab”. Therefore, length = 3
Longest palindromic substring ending at index 3 is “aba”. Therefore, length = 3
Longest palindromic substring ending at index 4 is “babab”. Therefore, length = 5
Longest palindromic substring ending at index 5 is “ababa”. Therefore, length = 5
Input: S = “aaa”
Output: 3 2 3
Explanation:
Longest palindromic substring starting at index 0 is “aaa”. Therefore, length = 3
Longest palindromic substring starting at index 1 is “ab”. Therefore, length = 2
Longest palindromic substring ending at index 3 is: “aaa”. Therefore, length = 3
Approach: The idea to solve this problem is to traverse the string and for each index, check for the longest palindromic substring that can be formed with that index either as the starting index and the ending index of the palindromic substring. Follow the steps below to solve the problem:
- Initialize an array palLength[] to store the length of the longest palindromic substrings for each index.
- Traverse the string using a variable i and perform the following operations:
- Initialize a variable, say maxLength, to store the length of the longest palindromic substring for each index.
- Consider i to be the ending index of a palindromic substring and find the first index from j over the range [0, i – 1], such that S[j, i] is a palindrome. Update maxLength.
- Consider i as the starting index of a palindromic substring and find the last index from j over the range [N – 1, i + 1], such that S[i, j] is a palindrome. Update maxLength.
- Store the maximum length obtained by storing the value of maxLength in palLength[i].
- After completing the above steps, print the array palLength[] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string S, int i, int j)
{
while (i < j)
{
if (S[i] != S[j])
return false ;
i++;
j--;
}
return true ;
}
void printLongestPalindrome(string S, int N)
{
int palLength[N];
for ( int i = 0; i < N; i++)
{
int maxlength = 1;
for ( int j = 0; j < i; j++)
{
if (S[j] == S[i])
{
if (isPalindrome(S, j, i))
{
maxlength = i - j + 1;
break ;
}
}
}
for ( int j = N - 1; j > i; j--)
{
if (S[j] == S[i])
{
if (isPalindrome(S, i, j))
{
maxlength = max(j - i + 1, maxlength);
break ;
}
}
}
palLength[i] = maxlength;
}
for ( int i = 0; i < N; i++)
{
cout << palLength[i] << " " ;
}
}
int main()
{
string S = "bababa" ;
int N = S.length();
printLongestPalindrome(S, N);
return 0;
}
|
Java
class GFG {
public static void
printLongestPalindrome(String S,
int N)
{
int palLength[] = new int [N];
for ( int i = 0 ; i < N; i++) {
int maxlength = 1 ;
for ( int j = 0 ; j < i; j++) {
if (S.charAt(j) == S.charAt(i)) {
if (isPalindrome(S, j, i)) {
maxlength = i - j + 1 ;
break ;
}
}
}
for ( int j = N - 1 ; j > i; j--) {
if (S.charAt(j) == S.charAt(i)) {
if (isPalindrome(S, i, j)) {
maxlength = Math.max(j - i + 1 ,
maxlength);
break ;
}
}
}
palLength[i] = maxlength;
}
for ( int i = 0 ; i < N; i++) {
System.out.print(palLength[i] + " " );
}
}
public static boolean isPalindrome(
String S, int i, int j)
{
while (i < j) {
if (S.charAt(i) != S.charAt(j))
return false ;
i++;
j--;
}
return true ;
}
public static void main(String[] args)
{
String S = "bababa" ;
int N = S.length();
printLongestPalindrome(S, N);
}
}
|
Python3
def isPalindrome(S, i, j):
while (i < j):
if (S[i] ! = S[j]):
return False
i + = 1
j - = 1
return True
def printLongestPalindrome(S, N):
palLength = [ 0 for i in range (N)]
for i in range (N):
maxlength = 1
for j in range (i):
if (S[j] = = S[i]):
if (isPalindrome(S, j, i)):
maxlength = i - j + 1
break
j = N - 1
while (j > i):
if (S[j] = = S[i]):
if (isPalindrome(S, i, j)):
maxlength = max (j - i + 1 , maxlength)
break
j - = 1
palLength[i] = maxlength
for i in range (N):
print (palLength[i],end = " " )
if __name__ = = '__main__' :
S = "bababa"
N = len (S)
printLongestPalindrome(S, N)
|
C#
using System;
class GFG
{
static bool isPalindrome( string S, int i, int j)
{
while (i < j)
{
if (S[i] != S[j])
return false ;
i++;
j--;
}
return true ;
}
static void printLongestPalindrome( string S, int N)
{
int [] palLength = new int [N];
for ( int i = 0; i < N; i++)
{
int maxlength = 1;
for ( int j = 0; j < i; j++)
{
if (S[j] == S[i])
{
if ((isPalindrome(S, j, i)) != false )
{
maxlength = i - j + 1;
break ;
}
}
}
for ( int j = N - 1; j > i; j--)
{
if (S[j] == S[i])
{
if (isPalindrome(S, i, j))
{
maxlength = Math.Max(j - i + 1, maxlength);
break ;
}
}
}
palLength[i] = maxlength;
}
for ( int i = 0; i < N; i++)
{
Console.Write(palLength[i] + " " );
}
}
static public void Main ()
{
string S = "bababa" ;
int N = S.Length;
printLongestPalindrome(S, N);
}
}
|
Javascript
<script>
function isPalindrome(S, i, j) {
while (i < j) {
if (S[i] !== S[j]) return false ;
i++;
j--;
}
return true ;
}
function printLongestPalindrome(S, N) {
var palLength = new Array(N);
for ( var i = 0; i < N; i++) {
var maxlength = 1;
for ( var j = 0; j < i; j++) {
if (S[j] === S[i]) {
if (isPalindrome(S, j, i) !== false ) {
maxlength = i - j + 1;
break ;
}
}
}
for ( var j = N - 1; j > i; j--) {
if (S[j] === S[i]) {
if (isPalindrome(S, i, j)) {
maxlength = Math.max(j - i + 1, maxlength);
break ;
}
}
}
palLength[i] = maxlength;
}
for ( var i = 0; i < N; i++) {
document.write(palLength[i] + " " );
}
}
var S = "bababa" ;
var N = S.length;
printLongestPalindrome(S, N);
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...