📌  相关文章
📜  每个前缀子串中 0 和 1 的数量相等且 1 的数量≥ 0 的数量的长度为 N 的二进制字符串的计数

📅  最后修改于: 2021-09-17 06:51:40             🧑  作者: Mango

给定一个整数N ,任务是 找到长度为N01的频率相等的可能二进制字符串的数量,其中1的频率大于或等于每个前缀子字符串中0的频率。

例子:

天真的方法:
最简单的方法是生成所有长度为 N 的二进制字符串并迭代每个字符串以检查它是否包含相等的01计数,并检查1的频率是否大于0在其所有前缀子串中

时间复杂度: O(N*2 ^N )
辅助空间: O(1)

有效的方法:
可以使用加泰罗尼亚数的概念进一步优化上述方法。我们只需要检查 N 的奇偶校验。

  • 如果N奇数,则 0 和 1 的频率不能相等。因此,此类所需字符串为0
  • 如果N偶数,则所需子串的数量等于第(N/2)加泰罗尼亚数字

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate and returns the
// value of Binomial Coefficient C(n, k)
unsigned long int binomialCoeff(unsigned int n,
                                unsigned int k)
{
    unsigned long int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to return the count of all
// binary strings having equal count of 0's
// and 1's and each prefix substring having
// frequency of 1's >= frequencies of 0's
unsigned long int countStrings(unsigned int N)
{
    // If N is odd
    if (N % 2 == 1)
 
        // No such strings possible
        return 0;
 
    // Otherwise
    else {
        N /= 2;
 
        // Calculate value of 2nCn
        unsigned long int c
            = binomialCoeff(2 * N, N);
 
        // Return 2nCn/(n+1)
        return c / (N + 1);
    }
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << countStrings(N) << " ";
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG{
 
// Function to calculate and returns the
// value of Binomial Coefficient C(n, k)
static long binomialCoeff(int n, int k)
{
    long res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to return the count of all
// binary strings having equal count of 0's
// and 1's and each prefix substring having
// frequency of 1's >= frequencies of 0's
static long countStrings(int N)
{
     
    // If N is odd
    if (N % 2 == 1)
 
        // No such strings possible
        return 0;
 
    // Otherwise
    else
    {
        N /= 2;
 
        // Calculate value of 2nCn
        long c = binomialCoeff(2 * N, N);
 
        // Return 2nCn/(n+1)
        return c / (N + 1);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 6;
     
    System.out.print(countStrings(N) + " ");
}
}
 
// This code is contributed by offbeat


Python3
# Python3 Program to implement
# the above approach
 
# Function to calculate and returns the
# value of Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
    res = 1
 
    # Since C(n, k) = C(n, n-k)
    if (k > n - k):
        k = n - k
 
    # Calculate the value of
    # [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
# Function to return the count of all
# binary strings having equal count of 0's
# and 1's and each prefix substring having
# frequency of 1's >= frequencies of 0's
def countStrings(N):
     
    # If N is odd
    if (N % 2 == 1):
 
        # No such strings possible
        return 0
 
    # Otherwise
    else:
        N //= 2
 
        # Calculate value of 2nCn
        c= binomialCoeff(2 * N, N)
 
        # Return 2nCn/(n+1)
        return c // (N + 1)
 
# Driver Code
if __name__ == '__main__':
    N = 6
    print(countStrings(N))
 
# This code is contributed by Mohit Kumar


C#
// C# program to implement the
// above approach
using System;
 
class GFG{
 
// Function to calculate and returns the
// value of Binomial Coefficient C(n, k)
static long binomialCoeff(int n, int k)
{
    long res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to return the count of all
// binary strings having equal count of 0's
// and 1's and each prefix substring having
// frequency of 1's >= frequencies of 0's
static long countStrings(int N)
{
     
    // If N is odd
    if (N % 2 == 1)
 
        // No such strings possible
        return 0;
 
    // Otherwise
    else
    {
        N /= 2;
 
        // Calculate value of 2nCn
        long c = binomialCoeff(2 * N, N);
 
        // Return 2nCn/(n+1)
        return c / (N + 1);
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 6;
     
    Console.Write(countStrings(N) + " ");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
5

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程