Minimum number of flipping adjacent bits required to make given Binary Strings equal | Function to find the minimum number of inversions required . ; Initializing the answer ; Iterate over the range ; If s1 [ i ] != s2 [ i ] , then inverse the characters at i snd ( i + 1 ) positions in s1 . ; Adding 1 to counter if characters are not same ; Driver Code
def find_Min_Inversion ( n , s1 , s2 ) : NEW_LINE INDENT count = 0 NEW_LINE s1 = list ( s1 ) NEW_LINE s2 = list ( s2 ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT if ( s1 [ i ] == '1' ) : NEW_LINE INDENT s1 [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT s1 [ i ] = '1' NEW_LINE DEDENT if ( s1 [ i + 1 ] == '1' ) : NEW_LINE INDENT s1 [ i + 1 ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT s1 [ i + 1 ] = '1' NEW_LINE DEDENT count += 1 NEW_LINE DEDENT DEDENT s1 = ' ' . join ( s1 ) NEW_LINE s2 = ' ' . join ( s2 ) NEW_LINE if ( s1 == s2 ) : NEW_LINE INDENT return count NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE s1 = "0101" NEW_LINE s2 = "1111" NEW_LINE print ( find_Min_Inversion ( n , s1 , s2 ) ) NEW_LINE DEDENT