Given an integer N, the task is to find the number of binary strings possible of length N having same frequency of 0s and 1s. If such string is possible of length N, print -1.
Note: Since the count can be very large, return the answer modulo 109+7.
Input: N = 2
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.
Strings “0011”, “0101”, “0110”, “1100”, “1010” and “1001” have same frequency of 0s and 1s.
Hence, the answer is 6.
The simplest approach is to generate all possible permutations of a string of length N having equal number of ‘0’ and ‘1’. For every permutation generated, increase the count. Print the total count of permutatiosn generated.
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
The above approach can be optimized by using concepts of Permutation and Combination. Follow the steps below to solve the problem:
- Since N positions need to be filled with equal number of 0‘s and 1‘s, select N/2 positions from the N positions in C(N, N/2) % mod( where mod = 109 + 7) ways to fill with only 1’s.
- Fill the remaining positions in C(N/2, N/2) % mod (i.e 1) way with only 0’s.
Below is the implementation of the above approach:
Time Complexity: O(N2)
Space Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of given length consisting of at least one 1
- Count number of binary strings of length N having only 0's and 1's
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of same length Strings that exists lexicographically in between two given Strings
- Count of strings that become equal to one of the two strings after one removal
- Count of sub-strings of length n possible from the given string
- Count of sub-strings with equal consecutive 0's and 1's
- Count of non-palindromic strings of length M using given N characters
- Count ways to increase LCS length of two strings by one
- Count the number of Special Strings of a given length N
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count the number of subsequences of length k having equal LCM and HCF
- Maximum count of sub-strings of length K consisting of same characters
- Count of unique palindromic strings of length X from given string
- Count the number of strings in an array whose distinct characters are less than equal to M
- Count of Binary Strings possible as per given conditions
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.