Minimize cost of flipping or swaps to make a Binary String balanced | Function to find the minimum cost to convert the given string into balanced string ; Stores count of 1 ' s ▁ and ▁ 0' s in the string ; Traverse the string ; Increment count1 ; Increment count 0 ; Stores absolute difference of counts of 0 ' s ▁ and ▁ 1' s ; If string consists of only 0 ' s ▁ and ▁ 1' s ; Print minimum cost ; Driver Code
function findMinimumCost ( s , N ) { let count_1 = 0 , count_0 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) count_1 ++ ; else count_0 ++ ; } let k = Math . abs ( count_0 - count_1 ) ; if ( count_1 == N count_0 == N ) document . write ( - 1 ) ; else document . write ( k / 2 ) ; } let S = " " ; let N = S . length ; findMinimumCost ( S , N ) ;