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++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++) {
res = res * i;
}
return res;
}
int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
void countWays(string s, string t)
{
int n = s.length();
int sum1 = 0, sum2 = 0, K = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '1' ) {
sum1++;
}
else
sum1--;
}
int m = t.length();
for ( int i = 0; i < m; i++) {
if (t[i] == '1' ) {
sum2++;
}
else if (t[i] == '0' ) {
sum2--;
}
else
K++;
}
int P = abs (sum1 - sum2);
if (P > K or (K - P) % 2) {
cout << 0;
return ;
}
cout << nCr(K, (P + K) / 2);
}
int main()
{
string S1 = "1010" ;
string S2 = "10??" ;
countWays(S1, S2);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class GFG{
static int fact( int n)
{
int res = 1 ;
for ( int i = 2 ; i <= n; i++) {
res = res * i;
}
return res;
}
static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static void countWays(String s, String t)
{
int n = s.length();
int sum1 = 0 , sum2 = 0 , K = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '1' ) {
sum1++;
}
else
sum1--;
}
int m = t.length();
for ( int i = 0 ; i < m; i++) {
if (t.charAt(i) == '1' ) {
sum2++;
}
else if (t.charAt(i) == '0' ) {
sum2--;
}
else
K++;
}
int P = Math.abs(sum1 - sum2);
if ((P > K) || (K - P) % 2 == 1 ) {
System.out.println( 0 );
return ;
}
System.out.println(nCr(K, (P + K) / 2 ));
}
public static void main(String[] args)
{
String S1 = "1010" ;
String S2 = "10??" ;
countWays(S1, S2);
}
}
|
Python3
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)
|
C#
using System;
class GFG{
static int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++) {
res = res * i;
}
return res;
}
static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static void countWays( string s, string t)
{
int n = s.Length;
int sum1 = 0, sum2 = 0, K = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '1' ) {
sum1++;
}
else
sum1--;
}
int m = t.Length;
for ( int i = 0; i < m; i++) {
if (t[i] == '1' ) {
sum2++;
}
else if (t[i] == '0' ) {
sum2--;
}
else
K++;
}
int P = Math.Abs(sum1 - sum2);
if ((P > K) || ((K - P) % 2) != 0) {
Console.WriteLine(0);
return ;
}
Console.WriteLine( nCr(K, (P + K) / 2));
}
static public void Main()
{
string S1 = "1010" ;
string S2 = "10??" ;
countWays(S1, S2);
}
}
|
Javascript
<script>
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] == '1' )
{
sum1++;
}
else
sum1--;
}
let m = t.length;
for (let i = 0; i < m; i++)
{
if (t[i] == '1' )
{
sum2++;
}
else if (t[i] == '0' )
{
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 = "1010" ;
let S2 = "10??" ;
countWays(S1, S2);
</script>
|
Time Complexity: O(max(N, M))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...