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 **10 ^{9} + 7**.

**Examples:**

Input:N = 4

Output:13

All possible valid strings are 0000, 0001, 0010, 0100,

1000, 0101, 0011, 1010, 1001, 0110, 1100, 1101 and 1011.

Input:N = 2

Output:4

**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 **i ^{th}** index and

**j**will be

**0, 1, 2, …, i**(may vary from

**1**to

**N**).

**dp[i][0] = dp[i – 1][0] + dp[i – 1][1] + dp[i – 1][2]**as in

**i**position,

**0**will be put.

**dp[i][1] = dp[i – 1][0]**as there is no

**1**at the

**(i – 1)**position so we take that value.

^{th}**dp[i][2] = dp[i – 1][1]**as first

**1**appeared at

**(i – 1)**position (consecutively) so we take that value directly.

^{th}The base cases are for length 1 string i.e.

**dp[1][0] = 1**,

**dp[1][1] = 1**,

**dp[1][2] = 0**. So, find all the value

**dp[N][0] + dp[N][1] + dp[N][2]**ans sum of all possible cases at the

**N**position.

^{th}Below is the implementation of the above approach:

## CPP

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `long` `MOD = 1000000007; ` ` ` `// Function to return the count of ` `// all possible binary strings ` `long` `countStr(` `long` `N) ` `{ ` ` ` ` ` `long` `dp[N + 1][3]; ` ` ` ` ` `// Fill 0's in the dp array ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// Base cases ` ` ` `dp[1][0] = 1; ` ` ` `dp[1][1] = 1; ` ` ` `dp[1][2] = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i <= N; i++) { ` ` ` ` ` `// dp[i][j] is the number of possible ` ` ` `// strings such that '1' just appeared ` ` ` `// consecutively j times upto ith index ` ` ` `dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] ` ` ` `+ dp[i - 1][2]) ` ` ` `% MOD; ` ` ` ` ` `// Taking previously calculated value ` ` ` `dp[i][1] = dp[i - 1][0] % MOD; ` ` ` `dp[i][2] = dp[i - 1][1] % MOD; ` ` ` `} ` ` ` ` ` `// Taking all the possible cases that ` ` ` `// can appear at the Nth position ` ` ` `long` `ans = (dp[N][0] + dp[N][1] + dp[N][2]) % MOD; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `N = 8; ` ` ` ` ` `cout << countStr(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `final` `static` `long` `MOD = ` `1000000007` `; ` ` ` ` ` `// Function to return the count of ` ` ` `// all possible binary strings ` ` ` `static` `long` `countStr(` `int` `N) ` ` ` `{ ` ` ` `long` `dp[][] = ` `new` `long` `[N + ` `1` `][` `3` `]; ` ` ` ` ` `// Fill 0's in the dp array ` ` ` `//memset(dp, 0, sizeof(dp)); ` ` ` ` ` `// Base cases ` ` ` `dp[` `1` `][` `0` `] = ` `1` `; ` ` ` `dp[` `1` `][` `1` `] = ` `1` `; ` ` ` `dp[` `1` `][` `2` `] = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= N; i++) ` ` ` `{ ` ` ` ` ` `// dp[i][j] is the number of possible ` ` ` `// strings such that '1' just appeared ` ` ` `// consecutively j times upto ith index ` ` ` `dp[i][` `0` `] = (dp[i - ` `1` `][` `0` `] + dp[i - ` `1` `][` `1` `] ` ` ` `+ dp[i - ` `1` `][` `2` `]) % MOD; ` ` ` ` ` `// Taking previously calculated value ` ` ` `dp[i][` `1` `] = dp[i - ` `1` `][` `0` `] % MOD; ` ` ` `dp[i][` `2` `] = dp[i - ` `1` `][` `1` `] % MOD; ` ` ` `} ` ` ` ` ` `// Taking all the possible cases that ` ` ` `// can appear at the Nth position ` ` ` `long` `ans = (dp[N][` `0` `] + dp[N][` `1` `] + dp[N][` `2` `]) % MOD; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `N = ` `8` `; ` ` ` ` ` `System.out.println(countStr(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` `MOD ` `=` `1000000007` ` ` `# Function to return the count of ` `# all possible binary strings ` `def` `countStr(N): ` ` ` ` ` `dp ` `=` `[[` `0` `for` `i ` `in` `range` `(` `3` `)] ` `for` `i ` `in` `range` `(N ` `+` `1` `)] ` ` ` ` ` `# Base cases ` ` ` `dp[` `1` `][` `0` `] ` `=` `1` ` ` `dp[` `1` `][` `1` `] ` `=` `1` ` ` `dp[` `1` `][` `2` `] ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, N ` `+` `1` `): ` ` ` ` ` `# dp[i][j] is the number of possible ` ` ` `# strings such that '1' just appeared ` ` ` `# consecutively j times upto ith index ` ` ` `dp[i][` `0` `] ` `=` `(dp[i ` `-` `1` `][` `0` `] ` `+` `dp[i ` `-` `1` `][` `1` `] ` `+` ` ` `dp[i ` `-` `1` `][` `2` `]) ` `%` `MOD ` ` ` ` ` `# Taking previously calculated value ` ` ` `dp[i][` `1` `] ` `=` `dp[i ` `-` `1` `][` `0` `] ` `%` `MOD ` ` ` `dp[i][` `2` `] ` `=` `dp[i ` `-` `1` `][` `1` `] ` `%` `MOD ` ` ` ` ` `# Taking all the possible cases that ` ` ` `# can appear at the Nth position ` ` ` `ans ` `=` `(dp[N][` `0` `] ` `+` `dp[N][` `1` `] ` `+` `dp[N][` `2` `]) ` `%` `MOD ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `8` ` ` ` ` `print` `(countStr(N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `long` `MOD = 1000000007; ` ` ` ` ` `// Function to return the count of ` ` ` `// all possible binary strings ` ` ` `static` `long` `countStr(` `int` `N) ` ` ` `{ ` ` ` `long` `[,]dp = ` `new` `long` `[N + 1, 3]; ` ` ` ` ` `// Base cases ` ` ` `dp[1, 0] = 1; ` ` ` `dp[1, 1] = 1; ` ` ` `dp[1, 2] = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i <= N; i++) ` ` ` `{ ` ` ` ` ` `// dp[i,j] is the number of possible ` ` ` `// strings such that '1' just appeared ` ` ` `// consecutively j times upto ith index ` ` ` `dp[i, 0] = (dp[i - 1, 0] + dp[i - 1, 1] ` ` ` `+ dp[i - 1, 2]) % MOD; ` ` ` ` ` `// Taking previously calculated value ` ` ` `dp[i, 1] = dp[i - 1, 0] % MOD; ` ` ` `dp[i, 2] = dp[i - 1, 1] % MOD; ` ` ` `} ` ` ` ` ` `// Taking all the possible cases that ` ` ` `// can appear at the Nth position ` ` ` `long` `ans = (dp[N, 0] + dp[N, 1] + dp[N, 2]) % MOD; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `Console.WriteLine(countStr(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

149

**Time 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.

## Recommended Posts:

- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- 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
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Maximum splits in binary string such that each substring is divisible by given odd number
- Longest Even Length Substring such that Sum of First and Second Half is same
- Minimum K such that every substring of length atleast K contains a character c
- Maximum length palindromic substring such that it starts and ends with given char
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Python | Check if there are K consecutive 1's in a binary number
- Check if there is any common character in two given strings
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.