Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2 | Function to find the length of the smallest string possible by removing substrings "01" and "11" ; Stores the length of the smallest string ; Traverse the string S ; If st is greater than 0 and S [ i ] is '1' ; Delete the last character and decrement st by 1 ; Otherwise ; Increment st by 1 ; Return the answer in st ; Input ; Function call
function shortestString ( S , N ) { let st = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( st > 0 && S [ i ] == ' ' ) { st -- ; } else { st ++ ; } } return st ; } let S = " " ; let N = S . length ; document . write ( shortestString ( S , N ) ) ;