Given an integer N, the task is to count the number of binary strings possible of length N such that they don’t contain “111” as a substring. The answer could be large so print answer modulo 109 + 7.
Input: N = 3
All possible substring are “000”, “001”,
“010”, “011”, “100”, “101” and “110”.
“111” is not a valid string.
Input N = 16
Approach: Dynamic programming can be used to solve this problem. Create a dp array where dp[i][j] will store the count of possible substrings such that 1 appears j times consecutively upto the ith index. Now, the recurrence relations will be:
dp[i] = dp[i – 1] + dp[i – 1] + dp[i – 1]
dp[i] = dp[i – 1]
dp[i] = dp[i – 1]
And the base cases will be dp = 1, dp = 1 and dp = 0. Now, the required count of strings will be dp[N] + dp[N] + dp[N].
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.
- Number of binary strings such that there is no substring of length ≥ 3
- 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 equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Smallest subarray of size greater than K with sum greater than a given value
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Find a number K such that exactly K array elements are greater than or equal to K
- Count of binary strings of length N having equal count of 0's and 1's
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count the number of words having sum of ASCII values less than and greater than k
- Length of longest subarray with product greater than or equal to 0
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Number of positions such that adding K to the element is greater than sum of all other elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.