Maximum count of “ 010. . ” subsequences that can be removed from given Binary String | 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
function countOperations ( S ) { let n = S . length ; let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' ' ) { cnt ++ ; } else { if ( cnt > 0 ) { cnt -- ; ans ++ ; } } } document . write ( ans ) ; } let S = " " ; countOperations ( S ) ;