Minimum characters that are to be inserted such that no three consecutive characters are same Last Updated : 15 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a string str and the task is to modify the string such that no three consecutive characters are the 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 Explanation: "aabbdbcc" is the modified string. Input: str = "geeksforgeeks" Output: 0 Explanation: There are no same consecutive characters Approach: The idea is to insert the character after the second character to minimize the operations. Follow the steps below to solve the problem: Loop over the string and check at ith index if str[i] == str[i + 1] and str[i] == str[i + 2]If this condition is true then increment the count. 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 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; } Java // 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 Python3 # 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 C# // 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 JavaScript <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> Output1 Time Complexity: O(N), Traversing over the string of size N.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum characters that are to be inserted such that no three consecutive characters are same K KunalGupta Follow Improve Article Tags : Strings Competitive Programming Data Structures DSA Constructive Algorithms +1 More Practice Tags : Data StructuresStrings Similar Reads Maximum length String so that no three consecutive characters are same Given the maximum occurrences of a, b, and c in a string, the task is to make the string containing only a, b, and c such that no three consecutive characters are the same. If the resultant string equals a+b+c, return the length (a+b+c) otherwise -1. Examples: Input: a = 3, b = 3, c = 3Output: 9Expl 4 min read Maximum count of X that can be inserted with no 3 adjacent characters are X Given a string, str of length N and a character X, the task is to find the maximum count of characters X that are to be inserted into the string such that no three consecutive characters are equal to X. If it is not possible to find such a string, then print -1. Examples: Input: str = âxxyxyâ, X = X 8 min read 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 sub 6 min read Longest subsequence such that no 3 consecutive characters are same Given a string of lowercase characters S, the task is to find longest subsequence of the string with no 3 consecutive identical characters.Examples: Input: S = "eedaaad"Output: eedaadExplanation: One occurrence of letter a is deleted. Input: xxxtxxxOutput: xxtxx Approach: The task can be solved by c 4 min read Minimum characters to be replaced to make frequency of all characters same Given a string S consisting of alphabets ['A' - 'Z'], the task is to find the minimum number of operations required to make frequency of every character equal. In one operation any character of the string can be chosen and replaced with another valid character.Examples: Input: S = "ABCB" Output: 1 E 6 min read Like