Skip to content
Related Articles

Related Articles

Remove longest prefix of the String which has duplicate substring

Improve Article
Save Article
  • Last Updated : 12 Apr, 2022
Improve Article
Save Article

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>


Output

a

Time Complexity: O(N2)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!