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:
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Number of binary strings such that there is no substring of length ≥ 3
- Count number of binary strings of length N having only 0's and 1's
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Count the number of subsequences of length k having equal LCM and HCF
- 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
- Length of the longest substring with equal 1s and 0s
- Count number of binary strings without consecutive 1's
- 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
- All possible binary numbers of length n with equal sum in both halves
- Count ways to increase LCS length of two strings by one
- Generate Binary Strings of length N using Branch and Bound
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
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.