Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
Last Updated :
13 Mar, 2022
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.
Examples:
Input: N = 3
Output: 7
All possible substring are "000", "001",
"010", "011", "100", "101" and "110".
"111" is not a valid string.
Input N = 16
Output: 19513
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][0] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2]
dp[i][1] = dp[i - 1][0]
dp[i][2] = dp[i - 1][1]
And the base cases will be dp[1][0] = 1, dp[1][1] = 1 and dp[1][2] = 0. Now, the required count of strings will be dp[N][0] + dp[N][1] + dp[N][2].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
const long MOD = 1000000007;
// Function to return the count
// of all possible valid strings
long countStrings(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] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 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 = 3;
cout << countStrings(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
final static int MOD = 1000000007;
// Function to return the count
// of all possible valid strings
static long countStrings(int N)
{
int i, j;
int dp[][] = new int[N + 1][3];
// Fill 0's in the dp array
for(i = 0; i < N + 1; i++)
{
for(j = 9; j < 3 ; j ++)
{
dp[i][j] = 0;
}
}
// Base cases
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (i = 2; i <= N; i++)
{
// dp[i][j] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 possible cases that
// can appear at the Nth position
int ans = (dp[N][0] + dp[N][1] +
dp[N][2]) % MOD;
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 3;
System.out.println(countStrings(N));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
MOD = 1000000007
# Function to return the count
# of all possible valid strings
def countStrings(N):
# Initialise and fill 0's in the dp array
dp = [[0] * 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] = number of possible strings
# such that '1' just appeared consecutively
# j times upto the 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 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 = 3
print(countStrings(N))
# This code is contributed by ashutosh450
C#
// C# implementation of the above approach
using System;
class GFG
{
static readonly int MOD = 1000000007;
// Function to return the count
// of all possible valid strings
static long countStrings(int N)
{
int i, j;
int [,]dp = new int[N + 1, 3];
// Fill 0's in the dp array
for(i = 0; i < N + 1; i++)
{
for(j = 9; j < 3; j ++)
{
dp[i, j] = 0;
}
}
// Base cases
dp[1, 0] = 1;
dp[1, 1] = 1;
dp[1, 2] = 0;
for (i = 2; i <= N; i++)
{
// dp[i,j] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 possible cases that
// can appear at the Nth position
int ans = (dp[N, 0] + dp[N, 1] +
dp[N, 2]) % MOD;
return ans;
}
// Driver code
public static void Main (String[] args)
{
int N = 3;
Console.WriteLine(countStrings(N));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// javascript implementation of the approach
var MOD = 1000000007;
// Function to return the count
// of all possible valid strings
function countStrings(N)
{
var i, j;
var dp = Array(N+1).fill(0).map(x => Array(3).fill(0));
// Fill 0's in the dp array
for(i = 0; i < N + 1; i++)
{
for(j = 9; j < 3 ; j ++)
{
dp[i][j] = 0;
}
}
// Base cases
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (i = 2; i <= N; i++)
{
// dp[i][j] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 possible cases that
// can appear at the Nth position
var ans = (dp[N][0] + dp[N][1] +
dp[N][2]) % MOD;
return ans;
}
// Driver code
var N = 3;
document.write(countStrings(N));
// This code is contributed by 29AjayKumar
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Number of binary strings such that there is no substring of length ⥠3 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.Examples: Input: N = 4 Output: 13 All possible valid strings are 0000, 0001, 0010, 0100, 1000,
10 min read
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 Given binary string str, the task is to count the number of substrings of the given string str such that each character of the substring belongs to a palindromic substring of length at least 2. Examples: Input: S = "00111" Output: 6 Explanation: There are 6 such substrings in the given string such t
6 min read
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read
Split the binary string into substrings with equal number of 0s and 1s Given a binary string str of length N, the task is to find the maximum count of consecutive substrings str can be divided into such that all the substrings are balanced i.e. they have equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then print -1.Example: Input
8 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha
12 min read