Minimum characters that are to be inserted such that no three consecutive characters are same - GeeksforGeeks

21 May, 2021
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++
Java
Python3
C#
Javascript
// 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 sameint 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 codeint main(){    string str = "aabbbcc";    int n = str.length();     cout << getCount(str, n);     return 0;}
// Java implementation of the approachimport 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
# 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 samedef 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 codestr1 = "aabbbcc"n = len(str1) print(getCount(str1, n)) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing 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
<script>      // JavaScript implementation of the above approach      // Function to return the count of characters      // that are to be inserted in str such that no      // three consecutive characters are same      function getCount(str, n) {        // To store the count of        // operations required        var cnt = 0;         var 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      var str = "aabbbcc";      var n = str.length;       document.write(getCount(str, n));    </script>
1
 
princiraj1992
ankthon
mohit kumar 29
rdtank
Constructive Algorithms
Data Structures
Strings
Data Structures
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction to Tree Data Structure
Static Data Structure vs Dynamic Data Structure
TCS NQT Coding Sheet
Building an undirected graph and finding shortest path using Dictionaries in Python
Program to implement Singly Linked List in C++ using class
Longest Common Subsequence | DP-4
Reverse a string in Java
Write a program to print all permutations of a given string
KMP Algorithm for Pattern Searching
C++ Data Types