计算替换“?”的方法在二进制字符串中使 0 和 1 的计数与另一个字符串的计数相同
给定两个大小分别为N和M的二进制字符串S1和S2 ,使得字符串S2也包含字符'?' ,任务是找到替换“?”的方法的数量。在字符串S2中,使得字符串S2中0和1的计数与字符串S1中的计数相同。
例子:
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
方法:给定的问题可以通过使用组合的概念来解决。请按照以下步骤解决问题:
- 初始化变量,例如sum1为0 , sum2为0 ,它们存储给定字符串S1和S2中0和1的数量。
- 初始化一个变量,比如说 作为0存储替换“?”的方式总数在满足给定条件的字符串S2中。
- 遍历字符串S1 ,如果当前字符为1 ,则将sum1的值递增 1. 否则,将sum1的值减1 。
- 遍历字符串S2 ,如果当前字符为1则将sum2的值加1或者如果当前字符为0则将sum2的值减1 。
- 遍历字符串t ,如果当前字符是'+'则将 sum2的值增加1 。否则,将K的值增加1 。
- 初始化一个变量,比如P存储sum1和sum2的绝对差。
- 如果P的值至少为 K或(K – P)的值是奇数,则没有可能的方法来替换“?”因此打印0 。否则,打印K C (P+K)/2的值作为路数的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
2
时间复杂度: O(max(N, M))
辅助空间: O(1)