Minimum number of flips or swaps of adjacent characters required to make two strings equal | Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Iterate rate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Given Input ; Function Call
def countMinSteps ( A , B , N ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( A [ i - 1 ] == B [ i - 1 ] ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 1 NEW_LINE DEDENT if ( i >= 2 and A [ i - 2 ] == B [ i - 1 ] and A [ i - 1 ] == B [ i - 2 ] ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , dp [ i - 2 ] + 1 ) NEW_LINE DEDENT DEDENT return dp [ N ] NEW_LINE DEDENT A = "0101" NEW_LINE B = "0011" NEW_LINE N = len ( A ) NEW_LINE print ( countMinSteps ( A , B , N ) ) NEW_LINE