Count of groups of consecutive 1 s in a given Binary String | Function to find the number of the groups of 1 s only in the binary string ; Stores number of groups of 1 s ; Initialization of the stack ; Traverse the string S ; If S [ i ] is '1' ; Otherwise ; If st is empty ; If st is not empty ; Return answer ; Input ; Function call
function groupsOfOnes ( S , N ) { let count = 0 ; var st = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) st . push ( 1 ) ; else { if ( st . length != 0 ) { count ++ ; while ( st . length != 0 ) { st . pop ( ) ; } } } } if ( st . length != 0 ) count ++ ; return count ; } var S = " " ; let N = S . length ; document . write ( groupsOfOnes ( S , N ) ) ;