Minimum number of elements to be removed so that pairwise consecutive elements are same | Java implementation of the above approach ; Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code
class GFG { static int countConsecutive ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { i ++ ; } else { count ++ ; } } return count ; } public static void main ( String args [ ] ) { String str = "44522255" ; System . out . println ( countConsecutive ( str ) ) ; } }