给定一个整数N ,任务是 找到长度为N且0和1的频率相等的可能二进制字符串的数量,其中1的频率大于或等于每个前缀子字符串中0的频率。
例子:
Input: N = 2
Output: 1
Explanation:
All possible binary strings of length 2 are {“00”, “01”, “10” and “11”}.
Out of these 4 strings, only “01” and “10” have equal count of 0’s and 1’s.
Out of these two strings, only “10” contains more or equal numbers of 1’s than 0’s in every prefix substring.
Input: N = 4
Output: 2
Explanation :
All possible binary strings of length 4, satisfying the required conditions are “1100” and “1010”.
天真的方法:
最简单的方法是生成所有长度为 N 的二进制字符串并迭代每个字符串以检查它是否包含相等的0和1计数,并检查1的频率是否大于0在其所有前缀子串中。
时间复杂度: O(N*2 ^N )
辅助空间: O(1)
有效的方法:
可以使用加泰罗尼亚数的概念进一步优化上述方法。我们只需要检查 N 的奇偶校验。
- 如果N是奇数,则 0 和 1 的频率不能相等。因此,此类所需字符串为0 。
- 如果N是偶数,则所需子串的数量等于第(N/2)个加泰罗尼亚数字。
Illustration:
N = 2
Only possible string is “10“. Therefore, count is 1.
N = 4
Only possible strings are “1100” and “1010”. Therefore, count is 2.
N = 6
Only possible strings are “111000”, “110100”, “110010”, “101010” and “101100”.
Therefore the count is 5.
Hence, for each even value of N, it follows the sequence 1 2 5 14 ……
Hence, the series is that of Catalan numbers.
Therefore, it can be concluded that if N is an even integer, the count is equal to that of (N/2)th Catalan Number.
下面是上述方法的实现:
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live