Open In App

Longest substring whose any non-empty substring not prefix or suffix of given String

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S of length N, the task is to find the length of the longest substring X of the string S such that:

  • No non-empty substring of X is a prefix of S.
  • No non-empty substring of X is a suffix of S.
  • If no such string is possible, print −1.

Examples:

Input: S = “abcdefb”
Output: 4
Explanation: cdef is the substring satisfying the conditions.

Input: S = “cccc”
Output: -1
Explanation: No substring can satisfy the conditions.

 

Approach: To solve the problem follow the below observation:

Observations:

Since a prefix starts from a starting of string S and a suffix starts from ending of a string S. Therefore maximum length of substring can be length of string S – 2 and it cannot have elements which are at the starting or ending of the string.
So select the substring not having any character which is the first or last character of the string. this will the largest substring satisfying the conditions.

Follow the steps mentioned to solve the problem:

  • Traverse the string from i = 1 to end of the string.
    • Check if any character is found which is not equal to first and last character of string.
    • If found consider it a part of the resultant substring.
    • Otherwise, it would not be included in the substring.
  • The result would be maximum length of substring satisfying the conditions.

Below is the implementation of the above approach: 

C++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the largest substring
int findLargest(string s)
{
  int maxi = -1;
  int sum = 0;
 
  // Loop to find the largest substring
  // satisfying the given conditions
  for (int i = 1; i < s.size() - 1; i++) {
    if (s[i] != s[0] && s[i] != s[s.length() - 1]) {
      sum++;
    }
    else {
      maxi = max(sum, maxi);
      sum = 0;
    }
  }
  maxi = max(sum, maxi);
  if (maxi == 0) {
    maxi = -1;
  }
 
  // Return the maximum length
  return maxi;
}
 
// Driver code
int main()
{
  string s = "abcdefb";
 
  // Function call
  cout << (findLargest(s));
 
  return 0;
}
 
// This code is contributed by rakeshsahni


Java




// Java code to implement the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the largest substring
    public static int findLargest(String s)
    {
        int max = -1;
        int sum = 0;
 
        // Loop to find the largest substring
        // satisfying the given conditions
        for (int i = 1; i < s.length() - 1; i++) {
            if (s.charAt(i) != s.charAt(0)
                && s.charAt(i)
                       != s.charAt(s.length() - 1)) {
                sum++;
            }
            else {
                max = Math.max(sum, max);
                sum = 0;
            }
        }
        max = Math.max(sum, max);
        if (max == 0) {
            max = -1;
        }
 
        // Return the maximum length
        return max;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "abcdefb";
 
        // Function call
        System.out.println(findLargest(s));
    }
}


Python




# Python code to implement the above approach
 
# Function to find the largest substring
def findLargest(s):
 
  maxi = -1
  sum = 0
 
  # Loop to find the largest substring
  # satisfying the given conditions
  for i in range(1, len(s) - 1):
    if (s[i] != s[0] and s[i] != s[len(s) - 1]):
      sum += 1
     
    else:
      maxi = max(sum, maxi)
      sum = 0
     
  maxi = max(sum, maxi)
  if (maxi == 0):
    maxi = -1
 
  # Return the maximum length
  return maxi
 
# Driver code
if __name__ == "__main__":
   
  s = "abcdefb"
 
  # Function call
  print(findLargest(s))
   
  # This code is contributed by hrithikgarg03188.


C#




// C# code to implement the above approach
using System;
 
public class GFG{
 
  // Function to find the largest substring
  public static int findLargest(string s)
  {
    int max = -1;
    int sum = 0;
 
    // Loop to find the largest substring
    // satisfying the given conditions
    for (int i = 1; i < s.Length - 1; i++) {
      if (s[i] != s[0] && s[i] != s[s.Length - 1]) {
        sum++;
      }
      else {
        max = Math.Max(sum, max);
        sum = 0;
      }
    }
    max = Math.Max(sum, max);
    if (max == 0) {
      max = -1;
    }
 
    // Return the maximum length
    return max;
  }
 
  // Driver code
  static public void Main (){
 
    string s = "abcdefb";
 
    // Function call
    Console.Write(findLargest(s));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
       // JavaScript code for the above approach
 
       // Function to find the largest substring
       function findLargest(s) {
           let maxi = -1;
           let sum = 0;
 
           // Loop to find the largest substring
           // satisfying the given conditions
           for (let i = 1; i < s.length - 1; i++) {
               if (s[i] != s[0] && s[i] != s[s.length - 1]) {
                   sum++;
               }
               else {
                   maxi = Math.max(sum, maxi);
                   sum = 0;
               }
           }
           maxi = Math.max(sum, maxi);
           if (maxi == 0) {
               maxi = -1;
           }
 
           // Return the maximum length
           return maxi;
       }
 
       // Driver code
       let s = "abcdefb";
 
       // Function call
       document.write(findLargest(s));
 
   // This code is contributed by Potta Lokesh
   </script>


Output

4

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



Last Updated : 11 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads