Minimum characters that are to be inserted such that no three consecutive characters are same | Java 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
import java . util . * ; class GFG { static int getCount ( char [ ] 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 ; } static public void main ( String [ ] arg ) { String str = " aabbbcc " ; int n = str . length ( ) ; System . out . println ( getCount ( str . toCharArray ( ) , n ) ) ; } }