Count ways to replace '?' in a Binary String to make the count of 0s and 1s same as that of another string
Last Updated :
30 Jul, 2021
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:
- 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.
- 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:
- Initialize variables say, sum1 as 0, sum2 as 0 that stores the number of 0s and 1s in the given string S1 and S2.
- Initialize a variable, say as 0 that stores the total count of ways to replace '?' in the string S2 satisfying the given criteria.
- Traverse the string S1, if the current character is 1, then increment the value of sum1 by 1. Otherwise, decrement the value of sum1 by 1.
- Traverse the string S2, if the current character is 1 then, increment the value of sum2 by 1 or if the current character is 0 then, decrement the value of sum2 by 1.
- Traverse the string t, if the current character is '+' increment the value of sum2 by 1. Otherwise, increment the value of K by 1.
- Initialize a variable, say P that stores the absolute difference of sum1 and sum2.
- If the value of P is at least K or the value of (K - P) is odd, then there are no possible ways to replace '?' and therefore print 0. Otherwise, print the value of KC(P+K)/2 as the resultant count of ways.
Below is the implementation of the above approach:
C++
// 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
// 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.
Python3
# 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#
// 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.
JavaScript
<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>
Time Complexity: O(max(N, M))
Auxiliary Space: O(1)
Similar Reads
Replace '?' to convert given string to a binary string with maximum count of '0' and "10" Given string str, consisting of three different types of characters '0', '1' and '?', the task is to convert the given string to a binary string by replacing the '?' characters with either '0' or '1' such that the count of 0s and 10 in the binary string is maximum. Examples: Input: str = 10?0?11Outp
4 min read
Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' Given a binary string S, the task is to count total binary strings consisting of at least one '1' of length equal to the length of the given string after repeatedly removing all occurrences of substrings "10" and "00" from the given string. Examples: Input: S = "111"Output: 7Explanation: Since there
4 min read
Split a Binary String such that count of 0s and 1s in left and right substrings is maximum Given a binary string, str of length N, the task is to find the maximum sum of the count of 0s on the left substring and count of 1s on the right substring possible by splitting the binary string into two non-empty substrings. Examples: Input: str = "000111" Output: 6 Explanation: Splitting the bina
7 min read
Minimize replacement of bits to make the count of 01 substring equal to 10 substring Given a binary string str. The task is to minimize the number of replacements of '0' by '1' or '1' by '0' to balance the binary string. A binary string is said to be balanced: "if the number of "01" substring = number of "10" substring". Examples: Input: str = "101010" Output: 1Explanation: "01" = 2
4 min read
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read