给定一个整数N ,任务是找到长度为N 且频率为0 s 和1 s 的可能二进制字符串的数量。如果这样的字符串长度为N ,则打印-1 。
注意:由于计数可能非常大,返回模10 9 +7的答案。
例子:
Input: N = 2
Output: 2
Explanation:
All possible binary strings of length 2 are “00”, “01”, “10” and “11”.
Among them, “10” and “01” have the same frequency of 0s and 1s.
Hence, the answer is 2.
Input: 4
Output: 6
Explanation:
Strings “0011”, “0101”, “0110”, “1100”, “1010” and “1001” have same frequency of 0s and 1s.
Hence, the answer is 6.
天真的方法:
最简单的方法是生成长度为N且‘0’和‘1’数量相等的字符串的所有可能排列。对于生成的每个排列,增加计数。打印生成置换的总数。
时间复杂度: O(N*N!)
辅助空间:O(N)
有效的方法:
上述方法可以通过使用排列和组合的概念进行优化。请按照以下步骤解决问题:
- 由于N 个位置需要填充相同数量的0和1 ,因此从N 个位置中选择N/2 个位置以C(N, N/2) % mod ( where 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。