human
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"
How can the above be solved in Python?
assistant
#Here is code to minimize swaps of same and the other steps required.
 def countSwaps ( S , T ) :
	value = [ 0 ] * 26
	for i in range ( 26 ) :
		value [ i ] = i + 1
	N = len ( S )
	sum1 = 0
	sum2 = 0
	flag = False
	for i in range ( N ) :
		sum1 += value [ ord ( S [ i ] ) - ord ( ' a ' ) ]
		sum2 += value [ ord ( T [ i ] ) - ord ( ' a ' ) ]
		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 ) :
			flag = False
	if ( sum1 % 2 == 1 and sum2 % 2 == 1 ) :
		print ( "0" )
	elif ( sum1 % 2 == 0 and sum2 % 2 == 0 ) :
		if ( flag ) :
			print ( "1" )
		else :
			print ( " - 1" )
	else :
		print ( " - 1" )
if __name__ == ' _ _ main _ _ ' :
	S = " acd "
	T = " dbf "
	countSwaps ( S , T )