Longest substring whose any non-empty substring not prefix or suffix of given String
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++
#include <bits/stdc++.h>
using namespace std;
int findLargest(string s)
{
int maxi = -1;
int sum = 0;
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 maxi;
}
int main()
{
string s = "abcdefb" ;
cout << (findLargest(s));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findLargest(String s)
{
int max = - 1 ;
int sum = 0 ;
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 max;
}
public static void main(String[] args)
{
String s = "abcdefb" ;
System.out.println(findLargest(s));
}
}
|
Python
def findLargest(s):
maxi = - 1
sum = 0
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 maxi
if __name__ = = "__main__" :
s = "abcdefb"
print (findLargest(s))
|
C#
using System;
public class GFG{
public static int findLargest( string s)
{
int max = -1;
int sum = 0;
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 max;
}
static public void Main (){
string s = "abcdefb" ;
Console.Write(findLargest(s));
}
}
|
Javascript
<script>
function findLargest(s) {
let maxi = -1;
let sum = 0;
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 maxi;
}
let s = "abcdefb" ;
document.write(findLargest(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
11 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...