Minimize removal of alternating subsequences to empty given Binary String | Java program for the above approach ; Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; Driver Code ; input string ; length of string ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static void minOpsToEmptyString ( String S , int N ) { int one = 0 , zero = 0 ; int x0 = 0 , x1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '0' ) { x0 ++ ; x1 = 0 ; } else { x1 ++ ; x0 = 0 ; } zero = Math . max ( x0 , zero ) ; one = Math . max ( x1 , one ) ; } System . out . println ( Math . max ( one , zero ) ) ; } public static void main ( String [ ] args ) { String S = "0100100111" ; int N = S . length ( ) ; minOpsToEmptyString ( S , N ) ; } }