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