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 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 contains all characters as '1 ; Minimum Cost ; Driver Code ; Given Input ; Function Call
' NEW_LINE def MinimumCost ( S , A , B ) : NEW_LINE INDENT count = 0 NEW_LINE group = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count > 0 ) : NEW_LINE INDENT group += 1 NEW_LINE DEDENT count = 0 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( count > 0 ) : NEW_LINE INDENT group += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( group == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( min ( A , B ) * ( group - 1 ) + B ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 1 NEW_LINE B = 5 NEW_LINE S = "01100" NEW_LINE MinimumCost ( S , A , B ) NEW_LINE DEDENT