Minimum flips required to convert given string into concatenation of equal substrings of length K | Function that returns the minimum number of flips to convert the s into a concatenation of K - length sub - string ; Stores the result ; Iterate through string index ; Stores count of 0 s & 1 s ; Iterate making K jumps ; Count 0 's ; Count 1 's ; Add minimum flips for index i ; Return minimum number of flips ; Driver code
def minOperations ( S , K ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT zero , one = 0 , 0 NEW_LINE for j in range ( i , len ( S ) , K ) : NEW_LINE INDENT if ( S [ j ] == '0' ) : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT one += 1 NEW_LINE DEDENT DEDENT ans += min ( zero , one ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "110100101" NEW_LINE K = 3 NEW_LINE print ( minOperations ( s , K ) ) NEW_LINE DEDENT