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

📅  最后修改于: 2022-05-13 01:56:10.702000             🧑  作者: Mango

生成所有长度为 N 且 0 和 1 计数相等的二进制字符串

给定一个整数N ,任务是生成所有等于01二进制字符串。如果没有字符串是可能的,打印-1

例子:

方法:可以使用递归来解决该任务。如果N奇数,则答案为-1 ,否则,我们可以使用递归来生成所有等于0 和 1 的二进制字符串。请按照以下步骤解决问题:

  • 变量 one 跟踪1 的数量,变量zeros跟踪字符串中0 的数量。
  • 一和应该有频率N/2
  • 基本条件:字符串s存储输出字符串。所以,当 s 的长度达到N我们停止递归调用并打印输出字符串s
  • 如果1 的频率小于N/2 ,则将1加到字符串中并增加 1
  • 如果0 的频率小于N/2 ,则将0添加到字符串并增加 zeros

下面是上述代码的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Recursive function that prints
// all strings of N length with equal 1's and 0's
void binaryNum(int n, string s, int ones,
               int zeros)
{
   
    // String s contains the output to be printed
    // ones stores the frequency of 1's
    // zeros stores the frequency of 0's
    // Base Condition: When the length of string s
    // becomes N
    if (s.length() == n)
    {
        cout << (s) << endl;
        return;
    }
 
    // If frequency of 1's is less than N/2 then
    // add 1 to the string and increment ones
    if (ones < n / 2)
        binaryNum(n, s + "1", ones + 1, zeros);
 
    // If frequency of 0's is less than N/2 then
    // add 0 to the string and increment zeros
    if (zeros < n / 2)
        binaryNum(n, s + "0", ones, zeros + 1);
}
 
// Driver Code
int main()
{
 
    string s = "";
    binaryNum(4, s, 0, 0);
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Recursive function that prints
    // all strings of N length with equal 1's and 0's
    static void binaryNum(int n, String s, int ones,
                          int zeros)
    {
        // String s contains the output to be printed
        // ones stores the frequency of 1's
        // zeros stores the frequency of 0's
        // Base Condition: When the length of string s
        // becomes N
        if (s.length() == n) {
            System.out.println(s);
            return;
        }
 
        // If frequency of 1's is less than N/2 then
        // add 1 to the string and increment ones
        if (ones < n / 2)
            binaryNum(n, s + "1", ones + 1, zeros);
 
        // If frequency of 0's is less than N/2 then
        // add 0 to the string and increment zeros
        if (zeros < n / 2)
            binaryNum(n, s + "0", ones, zeros + 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "";
        binaryNum(4, s, 0, 0);
    }
}


Python3
# python code for the above approach
 
# Recursive function that prints
# all strings of N length with equal 1's and 0's
def binaryNum(n, s, ones, zeros):
 
    # String s contains the output to be printed
    # ones stores the frequency of 1's
    # zeros stores the frequency of 0's
    # Base Condition: When the length of string s
    # becomes N
    if (len(s) == n):
 
        print(s)
        return
 
    # If frequency of 1's is less than N/2 then
    # add 1 to the string and increment ones
    if (ones < n / 2):
        binaryNum(n, s + "1", ones + 1, zeros)
 
    # If frequency of 0's is less than N/2 then
    # add 0 to the string and increment zeros
    if (zeros < n / 2):
        binaryNum(n, s + "0", ones, zeros + 1)
 
# Driver Code
if __name__ == "__main__":
 
    s = ""
    binaryNum(4, s, 0, 0)
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Recursive function that prints
    // all strings of N length with equal 1's and 0's
    static void binaryNum(int n, string s, int ones,
                          int zeros)
    {
        // String s contains the output to be printed
        // ones stores the frequency of 1's
        // zeros stores the frequency of 0's
        // Base Condition: When the length of string s
        // becomes N
        if (s.Length == n) {
            Console.WriteLine(s);
            return;
        }
 
        // If frequency of 1's is less than N/2 then
        // add 1 to the string and increment ones
        if (ones < n / 2)
            binaryNum(n, s + "1", ones + 1, zeros);
 
        // If frequency of 0's is less than N/2 then
        // add 0 to the string and increment zeros
        if (zeros < n / 2)
            binaryNum(n, s + "0", ones, zeros + 1);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string s = "";
        binaryNum(4, s, 0, 0);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
1100
1010
1001
0110
0101
0011

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