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++
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to delete the longest prefix
string delPrefix(string S)
{
if (S.size() == 1)
return S;
int i = 0, maxi = 0;
// Loop to find the
// longest duplicate of prefix
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);
}
// Driver code
int main()
{
string S = "aaaaa";
string ans = delPrefix(S);
// Function call
cout << ans;
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG
{
// Function to delete the longest prefix
public static String delPrefix(String S)
{
if (S.length() == 1)
return S;
int i = 0, maxi = 0;
// Loop to find the
// longest duplicate of prefix
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);
// Function call
System.out.print(ans);
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python code for the above approach:
# Function to delete the longest prefix
def delPrefix(S):
if (len(S) == 1):
return S
i = 0
maxi = 0
# Loop to find the
# longest duplicate of prefix
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:]
# Driver code
S = "aaaaa"
ans = delPrefix(S)
# Function call
print(ans)
# This code is contributed by Taranpreet
C#
// C# code for the above approach
using System;
public class GFG{
// Function to delete the longest prefix
public static string delPrefix(string S)
{
if (S.Length == 1)
return S;
int i = 0, maxi = 0;
// Loop to find the
// longest duplicate of prefix
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);
// Function call
Console.Write(ans);
}
}
// This code is contributed by hrithikgarg03188.
JavaScript
<script>
// JavaScript code for the above approach
function delPrefix(S) {
if (S.length == 1)
return S;
let i = 0, maxi = 0;
// Loop to find the
// longest duplicate of prefix
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);
}
// Driver code
let S = "aaaaa";
let ans = delPrefix(S);
// Function call
document.write(ans)
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Largest substring of str2 which is a prefix of str1 Given two string str1 and str2, the task is to find the longest prefix of str1 which is present as a substring of the string str2. Print the prefix if possible else print -1.Examples: Input: str1 = "geeksfor", str2 = "forgeeks" Output: geeks All the prefixes of str1 which are present in str2 are "g"
5 min read
Find the Longest Non-Prefix-Suffix Substring in the Given String Given a string s of length n. The task is to determine the longest substring t such that t is neither the prefix nor the suffix of string s, and that substring must appear as both prefix and suffix of the string s. If no such string exists, print -1. Example: Input: s = "fixprefixsuffix"Output: fix
7 min read
Print Longest substring without repeating characters Given a string s having lowercase characters, find the length of the longest substring without repeating characters. Examples:Input: s = âgeeksforgeeksâOutput: 7 Explanation: The longest substrings without repeating characters are âeksforgâ and âksforgeâ, with lengths of 7.Input: s = âaaaâOutput: 1E
14 min read
Remove characters from the first string which are present in the second string Given two strings string1 and string2, remove those characters from the first string(string1) which are present in the second string(string2). Both strings are different and contain only lowercase characters.NOTE: The size of the first string is always greater than the size of the second string( |st
15+ min read
Find the longest sub-string which is prefix, suffix and also present inside the string 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 =
9 min read
Find the longest sub-string which is prefix, suffix and also present inside the string 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 =
9 min read