Open In App

Count ways to replace ‘?’ in a Binary String to make the count of 0s and 1s same as that of another string

Given two binary strings S1 and S2 of size N and M respectively such that the string S2 also contains the character ‘?’, the task is to find the number of ways to replace ‘?’ in the string S2 such that the count of 0s and 1s in the string S2 is the same as in the string S1.

Examples:



Input: S1 = “1010”, S2 = “10??”
Output: 2
Explanation:
Following are the ways to replace ‘?’ in the string S2:

  1. Replace “??” in the string S2 with “01” modifies the string S2 = “1001”. Now the counts of 0s and 1s in both the strings S1 and S2 are the same.
  2. Replace “??” in the string S2 with “10” modifies the string S2 = “1010”. Now the counts of 0s and 1s in both the strings S1 and S2 are the same.

Therefore, the total number of ways is 2.



Input: S1 = “0000”, S2 = “??10?”
Output: 0

Approach: The given problem can be solved by using the concepts of Combinatorial. Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the factorial of
// the given number N
int fact(int n)
{
    // Stores the factorial
    int res = 1;
 
    // Iterate over the range [2, N]
    for (int i = 2; i <= n; i++) {
        res = res * i;
    }
 
    // Return the resultant result
    return res;
}
 
// Function to find the number of ways
// of choosing r objects from n
// distinct objects
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Function to find the number of ways
// to replace '?' in string t to get
// the same count of 0s and 1s in the
// string S and T
void countWays(string s, string t)
{
    int n = s.length();
    int sum1 = 0, sum2 = 0, K = 0;
 
    // Traverse the string s
    for (int i = 0; i < n; i++) {
 
        // If the current character
        // is 1
        if (s[i] == '1') {
 
            // Update the value of
            // the sum1
            sum1++;
        }
 
        // Otherwise
        else
            sum1--;
    }
 
    int m = t.length();
 
    // Traverse the string t
    for (int i = 0; i < m; i++) {
 
        // If the current character
        // is 1, then update the
        // value of sum2
        if (t[i] == '1') {
            sum2++;
        }
 
        // If the current character
        // is 0
        else if (t[i] == '0') {
            sum2--;
        }
 
        // Otherwise, update the
        // value of K
        else
            K++;
    }
 
    int P = abs(sum1 - sum2);
 
    // Check if P is greater than K
    // or if K-P is odd
    if (P > K or (K - P) % 2) {
        cout << 0;
        return;
    }
 
    // Print the count of ways
    cout << nCr(K, (P + K) / 2);
}
 
// Driver Code
int main()
{
    string S1 = "1010";
    string S2 = "10??";
    countWays(S1, S2);
 
    return 0;
}




// Java program for above approach
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
class GFG{
 
    // Function to find the factorial of
    // the given number N
    static int fact(int n)
    {
        // Stores the factorial
        int res = 1;
 
        // Iterate over the range [2, N]
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
 
        // Return the resultant result
        return res;
    }
 
    // Function to find the number of ways
    // of choosing r objects from n
    // distinct objects
    static int nCr(int n, int r)
    {
        return fact(n) / (fact(r) * fact(n - r));
    }
 
    // Function to find the number of ways
    // to replace '?' in string t to get
    // the same count of 0s and 1s in the
    // string S and T
    static void countWays(String s, String t)
    {
        int n = s.length();
        int sum1 = 0, sum2 = 0, K = 0;
 
        // Traverse the string s
        for (int i = 0; i < n; i++) {
 
            // If the current character
            // is 1
            if (s.charAt(i) == '1') {
 
                // Update the value of
                // the sum1
                sum1++;
            }
 
            // Otherwise
            else
                sum1--;
        }
 
        int m = t.length();
 
        // Traverse the string t
        for (int i = 0; i < m; i++) {
 
            // If the current character
            // is 1, then update the
            // value of sum2
            if (t.charAt(i) == '1') {
                sum2++;
            }
 
            // If the current character
            // is 0
            else if (t.charAt(i) == '0') {
                sum2--;
            }
 
            // Otherwise, update the
            // value of K
            else
                K++;
        }
 
        int P = Math.abs(sum1 - sum2);
 
        // Check if P is greater than K
        // or if K-P is odd
        if ((P > K) || (K - P) % 2==1) {
            System.out.println(0);
        return;
    }
 
        // Print the count of ways
        System.out.println(nCr(K, (P + K) / 2));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S1 = "1010";
        String S2 = "10??";
        countWays(S1, S2);
    }
}
 
// This code is contributed by hritikrommie.




# Python 3 program for the above approach
 
# Function to find the factorial of
# the given number N
def fact(n):
    # Stores the factorial
    res = 1
 
    # Iterate over the range [2, N]
    for i in range(2,n+1,1):
        res = res * i
 
    # Return the resultant result
    return res
 
# Function to find the number of ways
# of choosing r objects from n
# distinct objects
def nCr(n, r):
    return fact(n) // (fact(r) * fact(n - r))
 
# Function to find the number of ways
# to replace '?' in string t to get
# the same count of 0s and 1s in the
# string S and T
def countWays(s, t):
    n = len(s);
    sum1 = 0
    sum2 = 0
    K = 0
 
    # Traverse the string s
    for i in range(n):
        # If the current character
        # is 1
        if (s[i] == '1'):
 
            # Update the value of
            # the sum1
            sum1 += 1
 
        # Otherwise
        else:
            sum1 -= 1
 
    m = len(t)
 
    # Traverse the string t
    for i in range(m):
        # If the current character
        # is 1, then update the
        # value of sum2
        if (t[i] == '1'):
            sum2 += 1
     
        # If the current character
        # is 0
        elif (t[i] == '0'):
            sum2 -= 1
 
        # Otherwise, update the
        # value of K
        else:
            K += 1
 
    P = abs(sum1 - sum2)
 
    # Check if P is greater than K
    # or if K-P is odd
    if (P > K or (K - P) % 2):
        print(0)
        return
 
    # Print the count of ways
    print(nCr(K, (P + K) // 2))
 
# Driver Code
if __name__ == '__main__':
    S1 = "1010"
    S2 = "10??"
    countWays(S1, S2)
     
    # This code is contributed by ipg2016107.




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the factorial of
// the given number N
static int fact(int n)
{
    // Stores the factorial
    int res = 1;
 
    // Iterate over the range [2, N]
    for (int i = 2; i <= n; i++) {
        res = res * i;
    }
 
    // Return the resultant result
    return res;
}
 
// Function to find the number of ways
// of choosing r objects from n
// distinct objects
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Function to find the number of ways
// to replace '?' in string t to get
// the same count of 0s and 1s in the
// string S and T
static void countWays(string s, string t)
{
    int n = s.Length;
    int sum1 = 0, sum2 = 0, K = 0;
 
    // Traverse the string s
    for (int i = 0; i < n; i++) {
 
        // If the current character
        // is 1
        if (s[i] == '1') {
 
            // Update the value of
            // the sum1
            sum1++;
        }
 
        // Otherwise
        else
            sum1--;
    }
 
    int m = t.Length;
 
    // Traverse the string t
    for (int i = 0; i < m; i++) {
 
        // If the current character
        // is 1, then update the
        // value of sum2
        if (t[i] == '1') {
            sum2++;
        }
 
        // If the current character
        // is 0
        else if (t[i] == '0') {
            sum2--;
        }
 
        // Otherwise, update the
        // value of K
        else
            K++;
    }
 
    int P = Math.Abs(sum1 - sum2);
 
    // Check if P is greater than K
    // or if K-P is odd
    if ((P > K) || ((K - P) % 2) != 0) {
        Console.WriteLine(0);
        return;
    }
 
    // Print the count of ways
    Console.WriteLine( nCr(K, (P + K) / 2));
}
 
 
// Driver code
static public void Main()
{
    string S1 = "1010";
    string S2 = "10??";
    countWays(S1, S2);
}
}
 
// This code is contributed by target_2.




<script>
 
// JavaScript program for the above approach
 
// Function to find the factorial of
// the given number N
function fact(n)
{
     
    // Stores the factorial
    let res = 1;
 
    // Iterate over the range [2, N]
    for(let i = 2; i <= n; i++)
    {
        res = res * i;
    }
 
    // Return the resultant result
    return res;
}
 
// Function to find the number of ways
// of choosing r objects from n
// distinct objects
function nCr(n, r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Function to find the number of ways
// to replace '?' in string t to get
// the same count of 0s and 1s in the
// string S and T
function countWays(s, t)
{
    let n = s.length;
    let sum1 = 0, sum2 = 0, K = 0;
 
    // Traverse the string s
    for(let i = 0; i < n; i++)
    {
         
        // If the current character
        // is 1
        if (s[i] == '1')
        {
             
            // Update the value of
            // the sum1
            sum1++;
        }
 
        // Otherwise
        else
            sum1--;
    }
 
    let m = t.length;
 
    // Traverse the string t
    for(let i = 0; i < m; i++)
    {
         
        // If the current character
        // is 1, then update the
        // value of sum2
        if (t[i] == '1')
        {
            sum2++;
        }
 
        // If the current character
        // is 0
        else if (t[i] == '0')
        {
            sum2--;
        }
 
        // Otherwise, update the
        // value of K
        else
            K++;
    }
 
    let P = Math.abs(sum1 - sum2);
 
    // Check if P is greater than K
    // or if K-P is odd
    if (P > K || (K - P) % 2)
    {
        document.write(0);
        return;
    }
 
    // Print the count of ways
    document.write(nCr(K, (P + K) / 2));
}
 
// Driver Code
let S1 = "1010";
let S2 = "10??";
 
countWays(S1, S2);
 
// This code is contributed by Potta Lokesh
 
</script>

Output: 
2

 

Time Complexity: O(max(N, M))
Auxiliary Space: O(1)


Article Tags :