Given a string **str** and the task is to modify the string such that no three consecutive characters are same. In a single operation, any character can be inserted at any position in the string. Find the minimum number of such operations required.

**Examples:**

Input :str = “aabbbcc”Output:1

“aabbdbcc” is the modified string.

Input:str = “geeksforgeeks”Output:0

**Approach:** For every three consecutive characters which are same, a single character has to be inserted in between them to make any three consecutive characters different. But the number of operations needs to be minimized so the character must be inserted after the second character. For example, if the string is “bbbb” then if the character is inserted at the second position i.e. “babbb” then there are still three consecutive same characters and another operation is required to resolve that but if the character was inserted at the third position i.e. “bbabb” then only a single operation is required.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to return the count of characters ` `// that are to be inserted in str such that no ` `// three consecutive characters are same ` `int` `getCount(string str, ` `int` `n) `
`{ ` ` ` ` ` `// To store the count of `
` ` `// operations required `
` ` `int` `cnt = 0; `
` ` ` ` `int` `i = 0; `
` ` `while` `(i < n - 2) { `
` ` ` ` `// A character needs to be `
` ` `// inserted after str[i + 1] `
` ` `if` `(str[i] == str[i + 1] `
` ` `&& str[i] == str[i + 2]) { `
` ` `cnt++; `
` ` `i = i + 2; `
` ` `} `
` ` ` ` `// Current three consecutive `
` ` `// characters are not same `
` ` `else`
` ` `i++; `
` ` `} `
` ` ` ` `return` `cnt; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `string str = ` `"aabbbcc"` `; `
` ` `int` `n = str.length(); `
` ` ` ` `cout << getCount(str, n); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of the approach ` `import` `java.util.*; `
` ` `class` `GFG `
`{ ` ` ` ` ` `// Function to return the count of characters `
` ` `// that are to be inserted in str such that no `
` ` `// three consecutive characters are same `
` ` `static` `int` `getCount(` `char` `[] str, ` `int` `n) `
` ` `{ `
` ` ` ` `// To store the count of `
` ` `// operations required `
` ` `int` `cnt = ` `0` `; `
` ` ` ` `int` `i = ` `0` `; `
` ` `while` `(i < n - ` `2` `) `
` ` `{ `
` ` ` ` `// A character needs to be `
` ` `// inserted after str[i + 1] `
` ` `if` `(str[i] == str[i + ` `1` `] && `
` ` `str[i] == str[i + ` `2` `]) `
` ` `{ `
` ` `cnt++; `
` ` `i = i + ` `2` `; `
` ` `} `
` ` ` ` `// Current three consecutive `
` ` `// characters are not same `
` ` `else` ` ` `{ `
` ` `i++; `
` ` `} `
` ` `} `
` ` `return` `cnt; `
` ` `} `
` ` ` ` `// Driver code `
` ` `static` `public` `void` `main(String[] arg) `
` ` `{ `
` ` `String str = ` `"aabbbcc"` `; `
` ` `int` `n = str.length(); `
` ` ` ` `System.out.println(getCount(str.toCharArray(), n)); `
` ` `} `
`} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` ` ` `# Function to return the count of characters ` `# that are to be inserted in str1 such that no ` `# three consecutive characters are same ` `def` `getCount(str1, n): `
` ` ` ` `# To store the count of `
` ` `# operations required `
` ` `cnt ` `=` `0` `; `
` ` ` ` `i ` `=` `0` `; `
` ` `while` `(i < n ` `-` `2` `): `
` ` ` ` `# A character needs to be `
` ` `# inserted after str1[i + 1] `
` ` `if` `(str1[i] ` `=` `=` `str1[i ` `+` `1` `] ` `and` ` ` `str1[i] ` `=` `=` `str1[i ` `+` `2` `]): `
` ` `cnt ` `+` `=` `1`
` ` `i ` `=` `i ` `+` `2`
` ` ` ` `# Current three consecutive `
` ` `# characters are not same `
` ` `else` `: `
` ` `i ` `+` `=` `1`
` ` ` ` `return` `cnt `
` ` `# Driver code ` `str1 ` `=` `"aabbbcc"`
`n ` `=` `len` `(str1) `
` ` `print` `(getCount(str1, n)) `
` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

`// C# implementation of the above approach ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` ` ` `// Function to return the count of characters `
` ` `// that are to be inserted in str such that no `
` ` `// three consecutive characters are same `
` ` `static` `int` `getCount(` `string` `str, ` `int` `n) `
` ` `{ `
` ` ` ` `// To store the count of `
` ` `// operations required `
` ` `int` `cnt = 0; `
` ` ` ` `int` `i = 0; `
` ` `while` `(i < n - 2) `
` ` `{ `
` ` ` ` `// A character needs to be `
` ` `// inserted after str[i + 1] `
` ` `if` `(str[i] == str[i + 1] && `
` ` `str[i] == str[i + 2]) `
` ` `{ `
` ` `cnt++; `
` ` `i = i + 2; `
` ` `} `
` ` ` ` `// Current three consecutive `
` ` `// characters are not same `
` ` `else`
` ` `{ `
` ` `i++; `
` ` `} `
` ` `} `
` ` `return` `cnt; `
` ` `} `
` ` ` ` `// Driver code `
` ` `static` `public` `void` `Main () `
` ` `{ `
` ` `string` `str = ` `"aabbbcc"` `; `
` ` `int` `n = str.Length; `
` ` ` ` `Console.WriteLine(getCount(str, n)); `
` ` `} `
`} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

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:

- Longest substring such that no three consecutive characters are same
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Find three element from given three arrays such that their sum is X | Set 2
- Check three or more consecutive identical characters or numbers
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Group consecutive characters of same type in a string
- Minimum characters to be replaced to make frequency of all characters same
- Remove three consecutive duplicates from string
- Minimum number of deletions so that no two consecutive are same
- Minimum number of digits to be removed so that no two consecutive digits are same
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Remove minimum characters from string to split it into three substrings under given constraints
- Check whether two strings contain same characters in same order
- Strings formed from given characters without any consecutive repeating characters
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s

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.