Minimize flips to make binary string as all 1 s by flipping characters in substring of size K repeatedly | Function to find the minimum number of operations required to convert all the characters to 1 by flipping the substrings of size K ; Stores the minimum number of operations required ; Traverse the string S ; If the character is 0 ; Flip the substrings of size K starting from i ; Increment the minimum count of operations required ; After performing the operations check if string S contains any 0 s ; If S contains only 1 's ; Driver Code
def minOperation ( St , K , N ) : NEW_LINE INDENT S = list ( St ) NEW_LINE min = 0 NEW_LINE i = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '0' and i + K <= N ) : NEW_LINE INDENT j = i NEW_LINE while ( j < i + K ) : NEW_LINE INDENT if ( S [ j ] == '1' ) : NEW_LINE INDENT S [ j ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT S [ j ] = '1' NEW_LINE DEDENT j += 1 NEW_LINE DEDENT min += 1 NEW_LINE DEDENT DEDENT temp = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT temp += 1 NEW_LINE if ( S [ i ] == '0' ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( temp == N ) : NEW_LINE INDENT print ( min ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT S = "00010110" NEW_LINE K = 3 NEW_LINE N = len ( S ) NEW_LINE minOperation ( S , K , N ) NEW_LINE