Print the longest prefix of the given string which is also the suffix of the same string
Last Updated :
01 May, 2023
Given string str, the task is to find the longest prefix which is also the suffix of the given string. The prefix and suffix should not overlap. If no such prefix exists then print -1.
Examples:
Input: str = “aabcdaabc”
Output: aabc
The string “aabc” is the longest
prefix which is also suffix.
Input: str = “aaaa”
Output: aa
Approach: The idea is to use the pre-processing algorithm of the KMP search. In this algorithm, we build lps array which stores the following values:
lps[i] = the longest proper prefix of pat[0..i]
which is also a suffix of pat[0..i].
We get the length using the above approach, then print the same number of characters from the front which is our answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LengthlongestPrefixSuffix(string s)
{
int n = s.length();
int lps[n];
lps[0] = 0;
int len = 0;
int i = 1;
while (i < n) {
if (s[i] == s[len]) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
int res = lps[n - 1];
return (res > n / 2) ? n / 2 : res;
}
string longestPrefixSuffix(string s)
{
int len = LengthlongestPrefixSuffix(s);
string prefix = "" ;
for ( int i = 0; i < len; i++)
prefix += s[i];
return prefix;
}
int main()
{
string s = "abcab" ;
string ans = longestPrefixSuffix(s);
if (ans == "" )
cout << "-1" ;
else
cout << ans;
return 0;
}
|
Java
class GfG
{
static int LengthlongestPrefixSuffix(String s)
{
int n = s.length();
int lps[] = new int [n];
lps[ 0 ] = 0 ;
int len = 0 ;
int i = 1 ;
while (i < n)
{
if (s.charAt(i) == s.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0 )
{
len = lps[len - 1 ];
}
else
{
lps[i] = 0 ;
i++;
}
}
}
int res = lps[n - 1 ];
return (res > n / 2 ) ? n / 2 : res;
}
static String longestPrefixSuffix(String s)
{
int len = LengthlongestPrefixSuffix(s);
String prefix = "" ;
for ( int i = 0 ; i < len; i++)
prefix += s.charAt(i);
return prefix;
}
public static void main(String[] args)
{
String s = "abcab" ;
String ans = longestPrefixSuffix(s);
if (ans == "" )
System.out.println( "-1" );
else
System.out.println(ans);
}
}
|
Python3
def LengthlongestPrefixSuffix(s):
n = len (s)
lps = [ 0 for i in range (n)]
len1 = 0
i = 1
while (i < n):
if (s[i] = = s[len1]):
len1 + = 1
lps[i] = len1
i + = 1
else :
if (len1 ! = 0 ):
len1 = lps[len1 - 1 ]
else :
lps[i] = 0
i + = 1
res = lps[n - 1 ]
if (res > int (n / 2 )):
return int (n / 2 )
else :
return res
def longestPrefixSuffix(s):
len1 = LengthlongestPrefixSuffix(s)
prefix = ""
for i in range (len1):
prefix + = s[i]
return prefix
if __name__ = = '__main__' :
s = "abcab"
ans = longestPrefixSuffix(s)
if (ans = = ""):
print ( "-1" )
else :
print (ans)
|
C#
using System;
class GfG
{
static int LengthlongestPrefixSuffix( string s)
{
int n = s.Length;
int []lps = new int [n];
lps[0] = 0;
int len = 0;
int i = 1;
while (i < n)
{
if (s[i] == s[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
int res = lps[n - 1];
return (res > n / 2) ? n / 2 : res;
}
static String longestPrefixSuffix( string s)
{
int len = LengthlongestPrefixSuffix(s);
string prefix = "" ;
for ( int i = 0; i < len; i++)
prefix += s[i];
return prefix;
}
public static void Main()
{
string s = "abcab" ;
string ans = longestPrefixSuffix(s);
if (ans == "" )
Console.WriteLine( "-1" );
else
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function LengthlongestPrefixSuffix(s)
{
var n = s.length;
var lps = Array.from({length: n}, (_, i) => 0);
lps[0] = 0;
var len = 0;
var i = 1;
while (i < n)
{
if (s.charAt(i) == s.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
var res = lps[n - 1];
return (res > n / 2) ? n / 2 : res;
}
function longestPrefixSuffix(s)
{
var len = LengthlongestPrefixSuffix(s);
var prefix = "" ;
for ( var i = 0; i < len; i++)
prefix += s.charAt(i);
return prefix;
}
var s = "abcab" ;
var ans = longestPrefixSuffix(s);
if (ans == "" )
document.write( "-1" );
else
document.write(ans);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times to build los array. Where N is the length of the string.
Auxiliary Space: O(N), as we are using extra space for the lps array. Where N is the length of the string.
Share your thoughts in the comments
Please Login to comment...