Minimize count of flips required such that no substring of 0 s have length exceeding K | Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current sub ; If current character is 0 ; Continue ongoing sub ; Start a new sub ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
def min_flips ( strr , k ) : NEW_LINE INDENT if ( len ( strr ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE cnt_zeros = 0 NEW_LINE for ch in strr : NEW_LINE INDENT if ( ch == '0' ) : NEW_LINE INDENT cnt_zeros += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt_zeros = 0 NEW_LINE DEDENT if ( cnt_zeros == k ) : NEW_LINE INDENT ans += 1 NEW_LINE cnt_zeros = 0 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = "11100000011" NEW_LINE k = 3 NEW_LINE print ( min_flips ( strr , k ) ) NEW_LINE DEDENT