📌  相关文章
📜  长度为N的二进制字符串计数为0和1相等

📅  最后修改于: 2021-06-25 15:16:13             🧑  作者: Mango

给定一个整数N ,任务是找到具有0 s和1 s相同频率的长度N的二进制字符串的数量。如果这样的字符串的长度为N ,则打印-1
注意:由于计数可能非常大,请以10 9 +7为模返回答案。
例子:

天真的方法:
最简单的方法是生成长度为N的,具有相同数量的‘0’‘1’的字符串的所有可能排列。对于生成的每个排列,增加计数。打印生成置换的总数
时间复杂度: O(N * N!)
辅助空间:O(N)
高效方法:
可以通过使用置换和组合的概念来优化上述方法。请按照以下步骤解决问题:

  • 由于N个位置需要被填充有相等数量的01的S,在C优先选择的N个位置N / 2个位置(N,N / 2)%MOD(其中mod = 10 9 + 7)的方式来只填1。
  • 仅以0填充C(N / 2,N / 2)%mod (即1)方式中的其余位置。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
#define MOD 1000000007
 
// Function to calculate C(n, r) % MOD
// DP based approach
int nCrModp(int n, int r)
{
    // Corner case
    if (n % 2 == 1) {
        return -1;
    }
 
    // Stores the last row
    // of Pascal's Triangle
    int C[r + 1];
 
    memset(C, 0, sizeof(C));
 
    // Initialize top row
    // of pascal triangle
    C[0] = 1;
 
    // Construct Pascal's Triangle
    // from top to bottom
    for (int i = 1; i <= n; i++) {
 
        // Fill current row with the
        // help of previous row
        for (int j = min(i, r); j > 0;
            j--)
 
            // C(n, j) = C(n-1, j)
            // + C(n-1, j-1)
            C[j] = (C[j] + C[j - 1])
                % MOD;
    }
 
    return C[r];
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << nCrModp(N, N / 2);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
final static int MOD = 1000000007;
     
// Function to calculate C(n, r) % MOD
// DP based approach
static int nCrModp(int n, int r)
{
 
    // Corner case
    if (n % 2 == 1)
    {
        return -1;
    }
 
    // Stores the last row
    // of Pascal's Triangle
    int C[] = new int[r + 1];
 
    Arrays.fill(C, 0);
 
    // Initialize top row
    // of pascal triangle
    C[0] = 1;
 
    // Construct Pascal's Triangle
    // from top to bottom
    for(int i = 1; i <= n; i++)
    {
 
        // Fill current row with the
        // help of previous row
        for(int j = Math.min(i, r);
                j > 0; j--)
 
            // C(n, j) = C(n-1, j)
            // + C(n-1, j-1)
            C[j] = (C[j] + C[j - 1]) % MOD;
    }
    return C[r];
}
 
// Driver Code
public static void main(String s[])
{
    int N = 6;
    System.out.println(nCrModp(N, N / 2));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to implement
# the above approach
MOD = 1000000007
 
# Function to calculate C(n, r) % MOD
# DP based approach
def nCrModp (n, r):
 
    # Corner case
    if (n % 2 == 1):
        return -1
 
    # Stores the last row
    # of Pascal's Triangle
    C = [0] * (r + 1)
 
    # Initialize top row
    # of pascal triangle
    C[0] = 1
 
    # Construct Pascal's Triangle
    # from top to bottom
    for i in range(1, n + 1):
 
        # Fill current row with the
        # help of previous row
        for j in range(min(i, r), 0, -1):
 
            # C(n, j) = C(n-1, j)
            # + C(n-1, j-1)
            C[j] = (C[j] + C[j - 1]) % MOD
 
    return C[r]
 
# Driver Code
N = 6
 
print(nCrModp(N, N // 2))
 
# This code is contributed by himanshu77


C#
// C# program for the above approach
using System;
 
class GFG{
     
static int MOD = 1000000007;
 
// Function to calculate C(n, r) % MOD
// DP based approach
static int nCrModp(int n, int r)
{
     
    // Corner case
    if (n % 2 == 1)
    {
        return -1;
    }
 
    // Stores the last row
    // of Pascal's Triangle
    int[] C = new int[r + 1];
 
    // Initialize top row
    // of pascal triangle
    C[0] = 1;
 
    // Construct Pascal's Triangle
    // from top to bottom
    for(int i = 1; i <= n; i++)
    {
 
        // Fill current row with the
        // help of previous row
        for(int j = Math.Min(i, r);
                j > 0; j--)
 
            // C(n, j) = C(n-1, j)
            // + C(n-1, j-1)
            C[j] = (C[j] + C[j - 1]) % MOD;
    }
    return C[r];
}
 
// Driver code
static void Main()
{
    int N = 6;
    Console.WriteLine(nCrModp(N, N / 2));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
20

时间复杂度: O(N 2 )
空间复杂度: O(N)