Maximize the number of times a character can be removed from substring 01 from given Binary String | Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Input ; Function call
function maxOperations ( S , N ) { let X = 0 ; let Y = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) break ; Y ++ ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } let S = " " ; let N = S . length ; document . write ( maxOperations ( S , N ) ) ;