Longest substring between any pair of occurrences ōf similar characters
Given a string S, the task is to find the length of the longest substring between any pair of occurrences of same character.
Examples:
Input: S = “accabbacc”
Output: 6
Explanation: The longest substring satisfying the required conditions is “cabbac”, which lies between S[1](= ‘c’) and s[8](= ‘c’).
Input: S = “aab”
Output: 0
Approach: Follow the steps below to solve the problem:
- Initialize two variables res and diff to store the length of the longest substring and the length of the current substring between pair of same characters respectively.
- Iterate over characters of the string from left to right.
- Iterate over the remaining string on the right, from right to left up to the current character.
- If two equal characters are obtained, i.e. S[i] = S[j], , store the length of the substring between them in diff.
- Update the value of res as max(res, diff). so that res stores the longest substring of required type obtained so far.
- After complete traversal of the string, print res as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestbetweenequalcharacters(string S)
{
int n = S.length();
int res = -1, diff = -1;
for ( int i = 0; i < n - 1; i++) {
for ( int j = n - 1; j > i; j--) {
if (S[i] == S[j]) {
diff = j - i - 1;
res = max(diff, res);
}
}
}
return res;
}
int main()
{
string s = "accabbacc" ;
cout << longestbetweenequalcharacters(s);
}
|
Java
class GFG{
static int longestbetweenequalcharacters(String S)
{
int n = S.length();
int res = - 1 , diff = - 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
for ( int j = n - 1 ; j > i; j--)
{
if (S.charAt(i) == S.charAt(j))
{
diff = j - i - 1 ;
res = Math.max(diff, res);
}
}
}
return res;
}
public static void main(String[] args)
{
String s = "accabbacc" ;
System.out.println(
longestbetweenequalcharacters(s));
}
}
|
Python3
def longestbetweenequalcharacters(S):
n = len (S)
res = - 1
diff = - 1
for i in range (n - 1 ):
for j in range (n - 1 , i, - 1 ):
if (S[i] = = S[j]):
diff = j - i - 1
res = max (diff, res)
return res
if __name__ = = '__main__' :
s = "accabbacc"
print (longestbetweenequalcharacters(s))
|
C#
using System;
class GFG{
static int longestbetweenequalcharacters(String S)
{
int n = S.Length;
int res = -1, diff = -1;
for ( int i = 0; i < n - 1; i++)
{
for ( int j = n - 1; j > i; j--)
{
if (S[i] == S[j])
{
diff = j - i - 1;
res = Math.Max(diff, res);
}
}
}
return res;
}
public static void Main()
{
string s = "accabbacc" ;
Console.WriteLine(
longestbetweenequalcharacters(s));
}
}
|
Javascript
<script>
function longestbetweenequalcharacters(S)
{
var n = S.length;
var res = -1, diff = -1;
for (i = 0; i < n - 1; i++)
{
for (j = n - 1; j > i; j--)
{
if (S.charAt(i) == S.charAt(j))
{
diff = j - i - 1;
res = Math.max(diff, res);
}
}
}
return res;
}
var s = "accabbacc" ;
document.write(
longestbetweenequalcharacters(s));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
22 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...