Generate all Binary Strings of length N with equal count of 0s and 1s
Last Updated :
29 Dec, 2021
Given an integer N, the task is to generate all the binary strings with equal 0s and 1s. If no strings are possible, print -1
Examples:
Input: N = 2
Output: “01”, “10”
Explanation: All possible binary strings of length 2 are: 01, 10, 11, 00. Out of these, only 2 have equal number of 0s and 1s
Input: 4
Output: “0011”, “0101”, “0110”, “1100”, “1010”, “1001”
Approach: The task can be solved by using recursion. If N is odd, then the answer is -1, else, we can use recursion to generate all the binary strings with equal 0s and 1s. Follow the below steps to solve the problem:
- Variable ones keep track of the number of 1’s and variable zeros keeps a track of the number of 0’s in the string.
- Both ones and zeros should have frequency N/2.
- Base condition: The string s stores the output string. So, when the length of s reaches N we stop recursive calls and print the output string s.
- If the frequency of 1’s is less than N/2 then add 1 to the string and increment ones.
- If the frequency of 0’s is less than N/2 then add 0 to the string and increment zeros.
Below is the implementation of the above code:
C++
#include <bits/stdc++.h>
using namespace std;
void binaryNum( int n, string s, int ones,
int zeros)
{
if (s.length() == n)
{
cout << (s) << endl;
return ;
}
if (ones < n / 2)
binaryNum(n, s + "1" , ones + 1, zeros);
if (zeros < n / 2)
binaryNum(n, s + "0" , ones, zeros + 1);
}
int main()
{
string s = "" ;
binaryNum(4, s, 0, 0);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void binaryNum( int n, String s, int ones,
int zeros)
{
if (s.length() == n) {
System.out.println(s);
return ;
}
if (ones < n / 2 )
binaryNum(n, s + "1" , ones + 1 , zeros);
if (zeros < n / 2 )
binaryNum(n, s + "0" , ones, zeros + 1 );
}
public static void main(String[] args)
{
String s = "" ;
binaryNum( 4 , s, 0 , 0 );
}
}
|
Python3
def binaryNum(n, s, ones, zeros):
if ( len (s) = = n):
print (s)
return
if (ones < n / 2 ):
binaryNum(n, s + "1" , ones + 1 , zeros)
if (zeros < n / 2 ):
binaryNum(n, s + "0" , ones, zeros + 1 )
if __name__ = = "__main__" :
s = ""
binaryNum( 4 , s, 0 , 0 )
|
C#
using System;
class GFG {
static void binaryNum( int n, string s, int ones,
int zeros)
{
if (s.Length == n) {
Console.WriteLine(s);
return ;
}
if (ones < n / 2)
binaryNum(n, s + "1" , ones + 1, zeros);
if (zeros < n / 2)
binaryNum(n, s + "0" , ones, zeros + 1);
}
public static void Main( string [] args)
{
string s = "" ;
binaryNum(4, s, 0, 0);
}
}
|
Javascript
<script>
function binaryNum(n, s, ones, zeros)
{
if (s.length == n) {
document.write(s+ "<br>" );
return ;
}
if (ones < n / 2)
binaryNum(n, s + "1" , ones + 1, zeros);
if (zeros < n / 2)
binaryNum(n, s + "0" , ones, zeros + 1);
}
var s = "" ;
binaryNum(4, s, 0, 0);
</script>
|
Output
1100
1010
1001
0110
0101
0011
Time Complexity: O(2N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...