Minimum operations required to convert a binary string to all 0 s or all 1 s | Java implementation of the approach ; Function to return the count of minimum operations required ; Increment count when consecutive characters are different ; Answer is rounding off the ( count / 2 ) to lower ; Driver code
class GFG { static int minOperations ( String str , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) count ++ ; } return ( count + 1 ) / 2 ; } public static void main ( String [ ] args ) { String str = "000111" ; int n = str . length ( ) ; System . out . println ( minOperations ( str , n ) ) ; } }