Print the longest palindromic prefix of a given string
Given a string str, the task is to find the longest palindromic prefix of the given string.
Examples:
Input: str = “abaac”
Output: aba
Explanation:
The longest prefix of the given string which is palindromic is “aba”.
Input: str = “abacabaxyz”
Output: abacaba
Explanation:
The prefixes of the given string which is palindromic are “aba” and “abacabaxyz”.
But the longest of among two is “abacabaxyz”.
Naive Approach: The idea is to generate all the substring of the given string from the starting index and check whether the substrings are palindromic or not. The palindromic string with a maximum length is the resultant string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LongestPalindromicPrefix(string s)
{
int n = s.length();
int max_len = 0;
for ( int len = 1; len <= n; len++) {
string temp = s.substr(0, len);
string temp2 = temp;
reverse(temp2.begin(), temp2.end());
if (temp == temp2) {
max_len = len;
}
}
cout << s.substr(0, max_len);
}
int main()
{
string str = "abaab" ;
LongestPalindromicPrefix(str);
}
|
Java
import java.util.*;
class GFG{
static void LongestPalindromicPrefix(String s)
{
int n = s.length();
int max_len = 0 ;
for ( int len = 1 ; len <= n; len++)
{
String temp = s.substring( 0 , len);
String temp2 = temp;
temp2 = reverse(temp2);
if (temp.equals(temp2))
{
max_len = len;
}
}
System.out.print(s.substring( 0 , max_len));
}
static String reverse(String input)
{
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String str = "abaab" ;
LongestPalindromicPrefix(str);
}
}
|
Python3
def LongestPalindromicPrefix(string):
n = len (string)
max_len = 0
for length in range ( 0 , n + 1 ):
temp = string[ 0 :length]
temp2 = temp
temp3 = temp2[:: - 1 ]
if temp = = temp3:
max_len = length
print (string[ 0 :max_len])
if __name__ = = '__main__' :
string = "abaac" ;
LongestPalindromicPrefix(string)
|
C#
using System;
class GFG{
static void longestPalindromicPrefix(String s)
{
int n = s.Length;
int max_len = 0;
for ( int len = 1; len <= n; len++)
{
String temp = s.Substring(0, len);
String temp2 = temp;
temp2 = reverse(temp2);
if (temp.Equals(temp2))
{
max_len = len;
}
}
Console.Write(s.Substring(0, max_len));
}
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
public static void Main(String[] args)
{
String str = "abaab" ;
longestPalindromicPrefix(str);
}
}
|
Javascript
<script>
function LongestPalindromicPrefix(s)
{
let n = s.length;
let max_len = 0;
for (let len = 1; len <= n; len++)
{
let temp = s.substring(0, len);
let temp2 = temp;
temp2 = reverse(temp2);
if (temp == temp2)
{
max_len = len;
}
}
document.write(s.substring(0, max_len));
}
function reverse(input)
{
let a = input.split( '' );
let l, r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
let temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return a.join( "" );
}
let str = "abaab" ;
LongestPalindromicPrefix(str);
</script>
|
Time Complexity: O(N2), where N is the length of the given string.
Efficient Approach: The idea is to use preprocessing algorithm KMP Algorithm. Below are the steps:
- Create a temporary string(say str2) which is:
str2 = str + '?' reverse(str);
- Create an array(say lps[]) of size of length of the string str2 which will store the longest palindromic prefix which is also a suffix of string str2.
- Update the lps[] by using preprocessing algorithm of KMP Search Algorithm.
- lps[length(str2) – 1] will give the length of the longest palindromic prefix string of the given string str.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LongestPalindromicPrefix(string str)
{
string temp = str + '?' ;
reverse(str.begin(), str.end());
temp += str;
int n = temp.length();
int lps[n];
fill(lps, lps + n, 0);
for ( int i = 1; i < n; i++) {
int len = lps[i - 1];
while (len > 0
&& temp[len] != temp[i]) {
len = lps[len - 1];
}
if (temp[i] == temp[len]) {
len++;
}
lps[i] = len;
}
cout << temp.substr(0, lps[n - 1]);
}
int main()
{
string str = "abaab" ;
LongestPalindromicPrefix(str);
}
|
Java
import java.util.*;
class GFG{
static void LongestPalindromicPrefix(String str)
{
String temp = str + '?' ;
str = reverse(str);
temp += str;
int n = temp.length();
int []lps = new int [n];
Arrays.fill(lps, 0 );
for ( int i = 1 ; i < n; i++)
{
int len = lps[i - 1 ];
while (len > 0 && temp.charAt(len) !=
temp.charAt(i))
{
len = lps[len - 1 ];
}
if (temp.charAt(i) == temp.charAt(len))
{
len++;
}
lps[i] = len;
}
System.out.print(temp.substring( 0 , lps[n - 1 ]));
}
static String reverse(String input)
{
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String str = "abaab" ;
LongestPalindromicPrefix(str);
}
}
|
Python3
def LongestPalindromicPrefix( Str ):
temp = Str + "?"
Str = Str [:: - 1 ]
temp = temp + Str
n = len (temp)
lps = [ 0 ] * n
for i in range ( 1 , n):
Len = lps[i - 1 ]
while ( Len > 0 and temp[ Len ] ! = temp[i]):
Len = lps[ Len - 1 ]
if (temp[i] = = temp[ Len ]):
Len + = 1
lps[i] = Len
print (temp[ 0 : lps[n - 1 ]])
if __name__ = = '__main__' :
Str = "abaab"
LongestPalindromicPrefix( Str )
|
C#
using System;
class GFG{
static void longestPalindromicPrefix(String str)
{
String temp = str + '?' ;
str = reverse(str);
temp += str;
int n = temp.Length;
int []lps = new int [n];
for ( int i = 1; i < n; i++)
{
int len = lps[i - 1];
while (len > 0 && temp[len] != temp[i])
{
len = lps[len - 1];
}
if (temp[i] == temp[len])
{
len++;
}
lps[i] = len;
}
Console.Write(temp.Substring(0, lps[n - 1]));
}
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" , a);
}
public static void Main(String[] args)
{
String str = "abaab" ;
longestPalindromicPrefix(str);
}
}
|
Javascript
<script>
function longestPalindromicPrefix(str)
{
let temp = str + '?' ;
str = reverse(str);
temp += str;
let n = temp.length;
let lps = new Array(n);
lps.fill(0);
for (let i = 1; i < n; i++)
{
let len = lps[i - 1];
while (len > 0 && temp[len] != temp[i])
{
len = lps[len - 1];
}
if (temp[i] == temp[len])
{
len++;
}
lps[i] = len;
}
document.write(temp.substring(0, lps[n - 1]));
}
function reverse(input)
{
let a = input.split( '' );
let l, r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
let temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return a.join( "" );
}
let str = "abaab" ;
longestPalindromicPrefix(str);
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N), where N is the length of the given string.
Last Updated :
07 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...