Given an integer N, the task is to count the number of binary strings possible such that there is no substring of length ≥ 3 of all 1’s. This count can become very large so print the answer modulo 109 + 7.
Input: N = 4
All possible valid strings are 0000, 0001, 0010, 0100,
1000, 0101, 0011, 1010, 1001, 0110, 1100, 1101 and 1011.
Input: N = 2
Approach: For every value from 1 to N, the only required strings are in which the number of substrings in which ‘1’ appears consecutively for just two times, one time or zero times. This can be calculated from 2 to N recursively. Dynamic programming can be used for memoization where dp[i][j] will store the number of possible strings such that 1 just appeared consecutively j times upto the ith index and j will be 0, 1, 2, …, i (may vary from 1 to N).
dp[i] = dp[i – 1] + dp[i – 1] + dp[i – 1] as in i position, 0 will be put.
dp[i] = dp[i – 1] as there is no 1 at the (i – 1)th position so we take that value.
dp[i] = dp[i – 1] as first 1 appeared at (i – 1)th position (consecutively) so we take that value directly.
The base cases are for length 1 string i.e. dp = 1, dp = 1, dp = 0. So, find all the value dp[N] + dp[N] + dp[N] ans sum of all possible cases at the Nth position.
Below is the implementation of the above approach:
Time Complexity: O(N)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count number of binary strings of length N having only 0's and 1's
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Number of Binary Strings of length N with K adjacent Set Bits
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate Binary Strings of length N using Branch and Bound
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Minimum length of substring whose rotation generates a palindromic substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Maximum splits in binary string such that each substring is divisible by given odd number
- Number of strings of length N with no palindromic sub string
- Count the number of Special Strings of a given length N
- Minimum number of Binary strings to represent a Number
- Count number of binary strings without consecutive 1's
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.