Longest substring such that no three consecutive characters are same
Given string str, the task is to find the length of the longest substring of str such that no three consecutive characters in the substring are same.
Examples:
Input: str = “baaabbabbb”
Output: 7
“aabbabb” is the required substring.
Input: str = “babba”
Output: 5
Given string itself is the longest substring.
Approach: The following steps can be followed to solve the problem:
- If the length of the given string is less than 3 then the length of the string will be the answer.
- Initialize temp and ans as 2 initially, since this is the minimum length of the longest substring when the length of the given string is greater than 2.
- Iterate in the string from 2 to N – 1 and increment temp by 1 if str[i] != str[i – 1] or str[i] != str[i – 2].
- Else re-initialize temp = 2 and ans = max(ans, temp).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLenSubStr(string& s)
{
if (s.length() < 3)
return s.length();
int temp = 2;
int ans = 2;
for ( int i = 2; i < s.length(); i++) {
if (s[i] != s[i - 1] || s[i] != s[i - 2])
temp++;
else {
ans = max(temp, ans);
temp = 2;
}
}
ans = max(temp, ans);
return ans;
}
int main()
{
string s = "baaabbabbb" ;
cout << maxLenSubStr(s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxLenSubStr(String s)
{
if (s.length() < 3 )
return s.length();
int temp = 2 ;
int ans = 2 ;
for ( int i = 2 ; i < s.length(); i++)
{
if (s.charAt(i) != s.charAt(i - 1 ) ||
s.charAt(i) != s.charAt(i - 2 ))
temp++;
else
{
ans = Math.max(temp, ans);
temp = 2 ;
}
}
ans = Math.max(temp, ans);
return ans;
}
public static void main(String[] args)
{
String s = "baaabbabbb" ;
System.out.println(maxLenSubStr(s));
}
}
|
Python3
def maxLenSubStr(s):
if ( len (s) < 3 ):
return len (s)
temp = 2
ans = 2
for i in range ( 2 , len (s)):
if (s[i] ! = s[i - 1 ] or s[i] ! = s[i - 2 ]):
temp + = 1
else :
ans = max (temp, ans)
temp = 2
ans = max (temp, ans)
return ans
s = "baaabbabbb"
print (maxLenSubStr(s))
|
C#
using System;
class GFG
{
static int maxLenSubStr(String s)
{
if (s.Length < 3)
return s.Length;
int temp = 2;
int ans = 2;
for ( int i = 2; i < s.Length; i++)
{
if (s[i] != s[i - 1] ||
s[i] != s[i - 2])
temp++;
else
{
ans = Math.Max(temp, ans);
temp = 2;
}
}
ans = Math.Max(temp, ans);
return ans;
}
static public void Main ()
{
String s = "baaabbabbb" ;
Console.Write(maxLenSubStr(s));
}
}
|
Javascript
<script>
function maxLenSubStr(s)
{
if (s.length < 3)
return s.length;
let temp = 2;
let ans = 2;
for (let i = 2; i < s.length; i++)
{
if (s[i] != s[i - 1] ||
s[i] != s[i - 2])
temp++;
else
{
ans = Math.max(temp, ans);
temp = 2;
}
}
ans = Math.max(temp, ans);
return ans;
}
let s = "baaabbabbb" ;
document.write(maxLenSubStr(s));
</script>
|
Time Complexity: O(N) where N is the length of the string.
Space Complexity: O(1)
Last Updated :
11 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...