Maximum count of “ 010. . ” subsequences that can be removed from given Binary String | Java program for the above approach ; Function to count the maximum number of operations performed on the string ; Size of the string ; Stores the maximum number of operations performed ; Stores the count of ' X ' occurred so far ; Traverse the string ; Check if current char is ' X ' ; Check if the value of cnt is greater than 0 ; Decrement the value of cnt ; Increment the value of ans ; Print the value of ans ; Driver code
import java . io . * ; class GFG { public static void countOperations ( String S ) { int n = S . length ( ) ; int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . charAt ( i ) == '0' ) { cnt ++ ; } else { if ( cnt > 0 ) { cnt -- ; ans ++ ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = "110011010" ; countOperations ( S ) ; } }