Minimum removal of subsequences of distinct consecutive characters required to empty a given string | Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Stores length of str ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code
function findMinOperationsReqEmpStr ( str ) { let cntOne = 0 ; let cntZero = 0 ; let N = str . length ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { if ( cntOne != 0 ) { cntOne -- ; } cntZero ++ ; } else { if ( cntZero != 0 ) { cntZero -- ; } cntOne ++ ; } } return ( cntOne + cntZero ) ; } let str = " " ; document . write ( findMinOperationsReqEmpStr ( str ) ) ;