Given string str. The task is to find the longest sub-string which is a prefix, a suffix, and a sub-string of the given string, str. If no such string exists then print -1.
Examples:
Input: str = “fixprefixsuffix”
Output: fix
“fix” is a prefix, suffix and present inside in the string too.
Input: str = “aaaa”
“aa” is a prefix, suffix and present inside the string.
Approach: Let us calculate the longest prefix suffix for all prefixes of string. longest prefix suffix lps[i] is maximal length of prefix that also is suffix of substring [0…i]. More about the longest prefix suffix you can see in a description of kmp algorithm.
The first possible answer is a prefix of length lps[n-1]. If lps[n-1] = 0, there is no solution. For checking the first possible answer you should iterate over lps[i]. If at least one of them equal to lps[n-1] (but not n-1th, of course) – you found the answer. The second possible answer is a prefix of length lps[lps[n-1]-1]. If lps[lps[n-1]-1] = 0, you also have no solution. Otherwise, you can be sure that the answer already found. This substring is a prefix and a suffix of our string. Also, it is a suffix of a prefix with length lps[n-1] that places inside of all strings. This solution works in O(n).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> compute_lps(string s)
{
int n = s.size();
vector<int> lps(n);
int len = 0;
lps[0] = 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++;
}
}
}
return lps;
}
void Longestsubstring(string s)
{
vector<int> lps = compute_lps(s);
int n = s.size();
if (lps[n - 1] == 0) {
cout << -1;
return;
}
for (int i = 0; i < n - 1; i++) {
if (lps[i] == lps[n - 1]) {
cout << s.substr(0, lps[i]);
return;
}
}
if (lps[lps[n - 1] - 1] == 0)
cout << -1;
else
cout << s.substr(0, lps[lps[n - 1] - 1]);
}
int main()
{
string s = "fixprefixsuffix";
Longestsubstring(s);
return 0;
}
|
Java
class GFG
{
static int [] compute_lps(String s)
{
int n = s.length();
int [] lps = new int [n];
int len = 0;
lps[0] = 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++;
}
}
}
return lps;
}
static void Longestsubstring(String s)
{
int [] lps = compute_lps(s);
int n = s.length();
if (lps[n - 1] == 0)
{
System.out.println(-1);
return;
}
for (int i = 0; i < n - 1; i++)
{
if (lps[i] == lps[n - 1])
{
System.out.println(s.substring(0, lps[i]));
return;
}
}
if (lps[lps[n - 1] - 1] == 0)
System.out.println(-1);
else
System.out.println(s.substring(0, lps[lps[n - 1] - 1]));
}
public static void main (String [] args)
{
String s = "fixprefixsuffix";
Longestsubstring(s);
}
}
|
Python3
def compute_lps(s):
n = len(s)
lps = [0 for i in range(n)]
Len = 0
lps[0] = 0
i = 1
while (i < n):
if (s[i] == s[Len]):
Len += 1
lps[i] = Len
i += 1
else:
if (Len != 0):
Len = lps[Len - 1]
else:
lps[i] = 0
i += 1
return lps
def Longestsubstring(s):
lps = compute_lps(s)
n = len(s)
if (lps[n - 1] == 0):
print(-1)
exit()
for i in range(0,n - 1):
if (lps[i] == lps[n - 1]):
print(s[0:lps[i]])
exit()
if (lps[lps[n - 1] - 1] == 0):
print(-1)
else:
print(s[0:lps[lps[n - 1] - 1]])
s = "fixprefixsuffix"
Longestsubstring(s)
|
C#
using System;
class GFG
{
static int [] compute_lps(string s)
{
int n = s.Length;
int [] lps = new int [n];
int len = 0;
lps[0] = 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++;
}
}
}
return lps;
}
static void Longestsubstring(string s)
{
int [] lps = compute_lps(s);
int n = s.Length;
if (lps[n - 1] == 0)
{
Console.WriteLine(-1);
return;
}
for (int i = 0; i < n - 1; i++)
{
if (lps[i] == lps[n - 1])
{
Console.WriteLine(s.Substring(0, lps[i]));
return;
}
}
if (lps[lps[n - 1] - 1] == 0)
Console.WriteLine(-1);
else
Console.WriteLine(s.Substring(0, lps[lps[n - 1] - 1]));
}
public static void Main ()
{
string s = "fixprefixsuffix";
Longestsubstring(s);
}
}
|
PHP
<?php
function compute_lps($s)
{
$n = strlen($s);
$lps = array();
$len = 0;
$lps[0] = 0;
$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++;
}
}
}
return $lps;
}
function Longestsubstring($s)
{
$lps = compute_lps($s);
$n = strlen($s);
if ($lps[$n - 1] == 0)
{
echo -1;
return;
}
for ($i = 0; $i < $n - 1; $i++)
{
if ($lps[$i] == $lps[$n - 1])
{
echo substr($s, 0, $lps[$i]);
return;
}
}
if ($lps[$lps[$n - 1] - 1] == 0)
echo -1;
else
echo substr($s, 0, $lps[$lps[$n - 1] - 1]);
}
$s = "fixprefixsuffix";
Longestsubstring($s);
?>
|
Javascript
<script>
function compute_lps(s)
{
var n = s.length;
var lps = Array(n);
var len = 0;
lps[0] = 0;
var 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++;
}
}
}
return lps;
}
function Longestsubstring( s)
{
var lps = compute_lps(s);
var n = s.length;
if (lps[n - 1] == 0) {
document.write( -1);
return;
}
for (var i = 0; i < n - 1; i++) {
if (lps[i] == lps[n - 1]) {
document.write( s.substring(0, lps[i]));
return;
}
}
if (lps[lps[n - 1] - 1] == 0)
document.write( -1);
else
document.write( s.substr(0, lps[lps[n - 1] - 1]));
}
var s = "fixprefixsuffix";
Longestsubstring(s);
</script>
|
Time Complexity : O(N)
Auxiliary Space: O(N), since N extra space has been taken.