human
Count ways to replace ' ? ' in a Binary String to make the count of 0 s and 1 s same as that of another string | Function to find the factorial of the given number N.
Stores the factorial.
Iterate over the range [ 2 , N ].
Return the resultant result.
Function to find the number of ways of choosing r objects from n distinct objects.
Function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string S and T.
Traverse the string s.
If the current character is 1.
Update the value of the sum1.
Otherwise.
Traverse the string t.
If the current character is 1 , then update the value of sum2.
If the current character is 0.
Otherwise , update the value of K.
Check if P is greater than K or if K - P is odd.
Print the count of ways.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count ways to replace ' .
 def fact ( n ) :
	res = 1
	for i in range ( 2 , n + 1 , 1 ) :
		res = res * i
	return res
def nCr ( n , r ) :
	return fact ( n ) // ( fact ( r ) * fact ( n - r ) )
def countWays ( s , t ) :
	n = len ( s ) ;
	sum1 = 0
	sum2 = 0
	K = 0
	for i in range ( n ) :
		if ( s [ i ] == '1' ) :
			sum1 += 1
		else :
			sum1 -= 1
	m = len ( t )
	for i in range ( m ) :
		if ( t [ i ] == '1' ) :
			sum2 += 1
		elif ( t [ i ] == '0' ) :
			sum2 -= 1
		else :
			K += 1
	P = abs ( sum1 - sum2 )
	if ( P > K or ( K - P ) % 2 ) :
		print ( 0 )
		return
	print ( nCr ( K , ( P + K ) // 2 ) )
if __name__ == ' _ _ main _ _ ' :
	S1 = "1010"
	S2 = "10 ? ? "
	countWays ( S1 , S2 )