Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings | Function to calculate minimum cost to convert all the characters of S to '1' ; Stores the result ; Stores the number of groups that have 0 as characters ; Traverse the string S ; If current character is '0' ; If count is greater than 0 ; Set the count to 0 ; If the last few consecutive characters are '0' ; If string contains all characters as '1' ; Minimum Cost ; Given Input ; Function Call
function MinimumCost ( S , A , B ) { let count = 0 ; let group = 0 ; for ( let i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] == ' ' ) { count += 1 ; } else { if ( count > 0 ) { group += 1 ; } count = 0 ; } } if ( count > 0 ) group += 1 ; if ( group == 0 ) { document . write ( 0 + " " ) ; } else { document . write ( Math . min ( A , B ) * ( group - 1 ) + B ) ; } } let A = 1 ; let B = 5 ; let S = " " ; MinimumCost ( S , A , B ) ;