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
function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { res = res * i ; } return res ; } function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function countWays ( s , t ) { let n = s . length ; let sum1 = 0 , sum2 = 0 , K = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { sum1 ++ ; } else sum1 -- ; } let m = t . length ; for ( let i = 0 ; i < m ; i ++ ) { if ( t [ i ] == ' ' ) { sum2 ++ ; } else if ( t [ i ] == ' ' ) { sum2 -- ; } else K ++ ; } let P = Math . abs ( sum1 - sum2 ) ; if ( P > K || ( K - P ) % 2 ) { document . write ( 0 ) ; return ; } document . write ( nCr ( K , ( P + K ) / 2 ) ) ; } let S1 = " " ; let S2 = " " ; countWays ( S1 , S2 ) ;