Remove longest prefix of the String which has duplicate substring
Last Updated :
12 Apr, 2022
Given a string S of length N, the task is to remove the longest prefix of the string which has at least one duplicate substring present in S.
Note: The duplicate substring cannot be the prefix itself
Examples:
Input: S = “GeeksforGeeks”
Output: “forGeeks”
Explanation: The longest substring which has a duplicate is “Geeks”.
After deleting this the remaining string becomes “forGeeks”.
Input: S = “aaaaa”
Output: “a”
Explanation: Here the longest prefix which has a duplicate substring is “aaaa”.
So after deleting this the remaining string is “a”.
Note that the whole string is not selected because then the duplicate string is the prefix itself.
Approach: The problem can be solved based on the following idea:
Find all the characters which can be a starting point of a substring which is a duplicate of the prefix. Then from these points find the duplicate of the longest prefix and delete that prefix.
Follow the illustration below for a better understanding
Illustration:
For example take S = “aaaaa”
The possible starting points can be at indices 1, 2, 3, 4.
For index 1: The possible duplicate of the prefix is “aaaa”.
It has length = 4
For index 2: The possible duplicate of the prefix is “aaa”.
It has length = 3
For index 3: The possible duplicate of the prefix is “aa”.
It has length = 2
For index 4: The possible duplicate of the prefix is “a”.
It has length = 1
So remove the prefix of length 4. Therefore the string becomes “a”
Follow the approach mentioned below to solve the problem:
- Use two pointers: one to the start of the prefix (say i which is 0 initially) and the other (say j)to find the starting point of all possible duplicate substrings.
- Iterate j from 1 to N:
- If S[j] matches S[i] then use a pointer (say k) and iterate both i and k to find the length of the substring starting from j which is duplicate of the prefix.
- Update the maximum length.
- Set i again to 0.
- Delete the maximum length prefix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string delPrefix(string S)
{
if (S.size() == 1)
return S;
int i = 0, maxi = 0;
for ( int j = 1; j < S.size(); j++) {
int k = j;
while (k < S.size() and S[k] == S[i]) {
k++;
i++;
}
maxi = max(maxi, i);
i = 0;
}
return S.substr(maxi);
}
int main()
{
string S = "aaaaa" ;
string ans = delPrefix(S);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static String delPrefix(String S)
{
if (S.length() == 1 )
return S;
int i = 0 , maxi = 0 ;
for ( int j = 1 ; j < S.length(); j++) {
int k = j;
while (k < S.length()
&& S.charAt(k) == S.charAt(i)) {
k++;
i++;
}
maxi = Math.max(maxi, i);
i = 0 ;
}
return S.substring(maxi);
}
public static void main(String[] args)
{
String S = "aaaaa" ;
String ans = delPrefix(S);
System.out.print(ans);
}
}
|
Python3
def delPrefix(S):
if ( len (S) = = 1 ):
return S
i = 0
maxi = 0
for j in ( 1 , len (S) + 1 ):
k = j
while (k < len (S) and S[k] = = S[i]):
k = k + 1
i = i + 1
maxi = max (maxi, i)
i = 0
return S[maxi:]
S = "aaaaa"
ans = delPrefix(S)
print (ans)
|
C#
using System;
public class GFG{
public static string delPrefix( string S)
{
if (S.Length == 1)
return S;
int i = 0, maxi = 0;
for ( int j = 1; j < S.Length; j++) {
int k = j;
while (k < S.Length
&& S[k] == S[i]) {
k++;
i++;
}
maxi = Math.Max(maxi, i);
i = 0;
}
return S.Substring(maxi);
}
static public void Main (){
string S = "aaaaa" ;
string ans = delPrefix(S);
Console.Write(ans);
}
}
|
Javascript
<script>
function delPrefix(S) {
if (S.length == 1)
return S;
let i = 0, maxi = 0;
for (let j = 1; j < S.length; j++) {
let k = j;
while (k < S.length && S[k] == S[i]) {
k++;
i++;
}
maxi = Math.max(maxi, i);
i = 0;
}
return S.slice(maxi);
}
let S = "aaaaa" ;
let ans = delPrefix(S);
document.write(ans)
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...