Given an integer N, the task is to count the number of binary strings of length N having only 0’s and 1’s.
Note: Since the count can be very large, return the answer modulo 10^9+7.
Explanation: The numbers are 00, 01, 11, 10. Hence the count is 4.
Explanation: The numbers are 000, 001, 011, 010, 111, 101, 110, 100. Hence the count is 8.
Approach: The problem can be easily solved by using Permutation and Combination. At each position of the string there can only be two possibilities, i.e., 0 or 1. Therefore, the total number of permutation of 0 and 1 in a string of length N is given by 2*2*2*…(N times), i.e., 2^N. The answer can be very large, hence modulo by 10^9+7 is returned.
Below is the implementation of the above approach:
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 of binary strings of length N having equal count of 0's and 1's
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Length of longest subarray having only K distinct Prime Numbers
- Count of same length Strings that exists lexicographically in between two given Strings
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Count of numbers having only 1 set bit in the range [0, n]
- Count of groups among N people having only one leader in each group
- Count of numbers having only one unset bit in a range [L,R]
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count the number of subsequences of length k having equal LCM and HCF
- Count of binary strings of given length consisting of at least one 1
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Length of the smallest number which is divisible by K and formed by using 1's only
- Count ways to split a Binary String into three substrings having equal count of zeros
- Number of binary strings such that there is no substring of length ≥ 3
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.