📜  在每个前缀子串中,长度为N的二进制字符串的计数等于0和1的计数,等于1的计数≥0的计数

📅  最后修改于: 2021-04-27 22:34:11             🧑  作者: Mango

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

例子:

天真的方法:
最简单的方法是生成所有长度为N的二进制字符串,并迭代每个字符串以检查它是否包含01的相等计数,还检查1的频率是否大于s的频率。所有前缀子串中都为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 possibel
        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 possibel
        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 possibel
        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 possibel
        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)