Minimize swaps of same | Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of S ; Sum of T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If ord ( S [ i ] ) anord ( ' a ) rd ( T [ i ] ) ▁ haord ( ' a ) different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists pr1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
def countSwaps ( S , T ) : NEW_LINE INDENT value = [ 0 ] * 26 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT value [ i ] = i + 1 NEW_LINE DEDENT N = len ( S ) NEW_LINE sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE flag = False NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum1 += value [ ord ( S [ i ] ) - ord ( ' a ' ) ] NEW_LINE sum2 += value [ ord ( T [ i ] ) - ord ( ' a ' ) ] NEW_LINE if ( value [ ord ( S [ i ] ) - ord ( ' a ' ) ] % 2 == 0 and value [ ord ( T [ i ] ) - ord ( ' a ' ) ] % 2 == 1 or value [ ord ( S [ i ] ) - ord ( ' a ' ) ] % 2 == 1 and value [ ord ( T [ i ] ) - ord ( ' a ' ) ] % 2 == 0 ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT DEDENT if ( sum1 % 2 == 1 and sum2 % 2 == 1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( sum1 % 2 == 0 and sum2 % 2 == 0 ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " acd " NEW_LINE T = " dbf " NEW_LINE countSwaps ( S , T ) NEW_LINE DEDENT