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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the length of the ` `// longest substring such that no three ` `// consecutive characters are same ` `int` `maxLenSubStr(string& s) ` `{ ` ` ` `// If the length of the given string ` ` ` `// is less than 3 ` ` ` `if` `(s.length() < 3) ` ` ` `return` `s.length(); ` ` ` ` ` `// Initialize temporary and final ans ` ` ` `// to 2 as this is the minimum length ` ` ` `// of substring when length of the given ` ` ` `// string is greater than 2 ` ` ` `int` `temp = 2; ` ` ` `int` `ans = 2; ` ` ` ` ` `// Traverse the string from the ` ` ` `// third character to the last ` ` ` `for` `(` `int` `i = 2; i < s.length(); i++) { ` ` ` ` ` `// If no three consecutive characters ` ` ` `// are same then increment temporary count ` ` ` `if` `(s[i] != s[i - 1] || s[i] != s[i - 2]) ` ` ` `temp++; ` ` ` ` ` `// Else update the final ans and ` ` ` `// reset the temporary count ` ` ` `else` `{ ` ` ` `ans = max(temp, ans); ` ` ` `temp = 2; ` ` ` `} ` ` ` `} ` ` ` ` ` `ans = max(temp, ans); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"baaabbabbb"` `; ` ` ` ` ` `cout << maxLenSubStr(s); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the length of the ` `// longest substring such that no three ` `// consecutive characters are same ` `static` `int` `maxLenSubStr(String s) ` `{ ` ` ` `// If the length of the given string ` ` ` `// is less than 3 ` ` ` `if` `(s.length() < ` `3` `) ` ` ` `return` `s.length(); ` ` ` ` ` `// Initialize temporary and final ans ` ` ` `// to 2 as this is the minimum length ` ` ` `// of substring when length of the given ` ` ` `// string is greater than 2 ` ` ` `int` `temp = ` `2` `; ` ` ` `int` `ans = ` `2` `; ` ` ` ` ` `// Traverse the string from the ` ` ` `// third character to the last ` ` ` `for` `(` `int` `i = ` `2` `; i < s.length(); i++) ` ` ` `{ ` ` ` ` ` `// If no three consecutive characters ` ` ` `// are same then increment temporary count ` ` ` `if` `(s.charAt(i) != s.charAt(i - ` `1` `) || ` ` ` `s.charAt(i) != s.charAt(i - ` `2` `)) ` ` ` `temp++; ` ` ` ` ` `// Else update the final ans and ` ` ` `// reset the temporary count ` ` ` `else` ` ` `{ ` ` ` `ans = Math.max(temp, ans); ` ` ` `temp = ` `2` `; ` ` ` `} ` ` ` `} ` ` ` `ans = Math.max(temp, ans); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String s = ` `"baaabbabbb"` `; ` ` ` ` ` `System.out.println(maxLenSubStr(s)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the length of the ` `# longest substring such that no three ` `# consecutive characters are same ` `def` `maxLenSubStr(s): ` ` ` ` ` `# If the length of the given string ` ` ` `# is less than 3 ` ` ` `if` `(` `len` `(s) < ` `3` `): ` ` ` `return` `len` `(s) ` ` ` ` ` `# Initialize temporary and final ans ` ` ` `# to 2 as this is the minimum length ` ` ` `# of substring when length of the given ` ` ` `# string is greater than 2 ` ` ` `temp ` `=` `2` ` ` `ans ` `=` `2` ` ` ` ` `# Traverse the string from the ` ` ` `# third character to the last ` ` ` `for` `i ` `in` `range` `(` `2` `, ` `len` `(s)): ` ` ` ` ` `# If no three consecutive characters ` ` ` `# are same then increment temporary count ` ` ` `if` `(s[i] !` `=` `s[i ` `-` `1` `] ` `or` `s[i] !` `=` `s[i ` `-` `2` `]): ` ` ` `temp ` `+` `=` `1` ` ` ` ` `# Else update the final ans and ` ` ` `# reset the temporary count ` ` ` `else` `: ` ` ` `ans ` `=` `max` `(temp, ans) ` ` ` `temp ` `=` `2` ` ` `ans ` `=` `max` `(temp, ans) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `s ` `=` `"baaabbabbb"` ` ` `print` `(maxLenSubStr(s)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the length of the ` `// longest substring such that no three ` `// consecutive characters are same ` `static` `int` `maxLenSubStr(String s) ` `{ ` ` ` `// If the length of the given string ` ` ` `// is less than 3 ` ` ` `if` `(s.Length < 3) ` ` ` `return` `s.Length; ` ` ` ` ` `// Initialize temporary and final ans ` ` ` `// to 2 as this is the minimum length ` ` ` `// of substring when length of the given ` ` ` `// string is greater than 2 ` ` ` `int` `temp = 2; ` ` ` `int` `ans = 2; ` ` ` ` ` `// Traverse the string from the ` ` ` `// third character to the last ` ` ` `for` `(` `int` `i = 2; i < s.Length; i++) ` ` ` `{ ` ` ` ` ` `// If no three consecutive characters ` ` ` `// are same then increment temporary count ` ` ` `if` `(s[i] != s[i - 1] || ` ` ` `s[i] != s[i - 2]) ` ` ` `temp++; ` ` ` ` ` `// Else update the final ans and ` ` ` `// reset the temporary count ` ` ` `else` ` ` `{ ` ` ` `ans = Math.Max(temp, ans); ` ` ` `temp = 2; ` ` ` `} ` ` ` `} ` ` ` `ans = Math.Max(temp, ans); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `String s = ` `"baaabbabbb"` `; ` ` ` `Console.Write(maxLenSubStr(s)); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** O(N) where N is the length of the string.

**Space Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum characters that are to be inserted such that no three consecutive characters are same
- Length of the longest substring with no consecutive same letters
- Longest substring with atmost K characters from the given set of characters
- Longest Even Length Substring such that Sum of First and Second Half is same
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Find three element from given three arrays such that their sum is X | Set 2
- Check three or more consecutive identical characters or numbers
- Longest substring of vowels with no two adjacent alphabets same
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Maximum length substring having all same characters after k changes
- Minimum steps to delete a string by deleting substring comprising of same characters
- Largest substring with same Characters
- Find the longest substring with k unique characters in a given string
- Print Longest substring without repeating characters
- Longest substring of only 4's from the first N characters of the infinite string
- Length of longest substring having all characters as K
- Longest substring with K unique characters using Binary Search
- Find length of longest substring with at most K normal characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.