Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1’s
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++
#include <bits/stdc++.h>
using namespace std;
const long MOD = 1000000007;
long countStrings( long N)
{
long dp[N + 1][3];
memset (dp, 0, sizeof (dp));
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for ( int i = 2; i <= N; i++) {
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]
+ dp[i - 1][2])
% MOD;
dp[i][1] = dp[i - 1][0] % MOD;
dp[i][2] = dp[i - 1][1] % MOD;
}
long ans = (dp[N][0] + dp[N][1]
+ dp[N][2])
% MOD;
return ans;
}
int main()
{
long N = 3;
cout << countStrings(N);
return 0;
}
|
Java
class GFG
{
final static int MOD = 1000000007 ;
static long countStrings( int N)
{
int i, j;
int dp[][] = new int [N + 1 ][ 3 ];
for (i = 0 ; i < N + 1 ; i++)
{
for (j = 9 ; j < 3 ; j ++)
{
dp[i][j] = 0 ;
}
}
dp[ 1 ][ 0 ] = 1 ;
dp[ 1 ][ 1 ] = 1 ;
dp[ 1 ][ 2 ] = 0 ;
for (i = 2 ; i <= N; i++)
{
dp[i][ 0 ] = (dp[i - 1 ][ 0 ] + dp[i - 1 ][ 1 ] +
dp[i - 1 ][ 2 ]) % MOD;
dp[i][ 1 ] = dp[i - 1 ][ 0 ] % MOD;
dp[i][ 2 ] = dp[i - 1 ][ 1 ] % MOD;
}
int ans = (dp[N][ 0 ] + dp[N][ 1 ] +
dp[N][ 2 ]) % MOD;
return ans;
}
public static void main (String[] args)
{
int N = 3 ;
System.out.println(countStrings(N));
}
}
|
Python3
MOD = 1000000007
def countStrings(N):
dp = [[ 0 ] * 3 for i in range (N + 1 )]
dp[ 1 ][ 0 ] = 1 ;
dp[ 1 ][ 1 ] = 1 ;
dp[ 1 ][ 2 ] = 0 ;
for i in range ( 2 , N + 1 ):
dp[i][ 0 ] = (dp[i - 1 ][ 0 ] +
dp[i - 1 ][ 1 ] +
dp[i - 1 ][ 2 ]) % MOD
dp[i][ 1 ] = dp[i - 1 ][ 0 ] % MOD
dp[i][ 2 ] = dp[i - 1 ][ 1 ] % MOD
ans = (dp[N][ 0 ] + dp[N][ 1 ] + dp[N][ 2 ]) % MOD
return ans
if __name__ = = '__main__' :
N = 3
print (countStrings(N))
|
C#
using System;
class GFG
{
static readonly int MOD = 1000000007;
static long countStrings( int N)
{
int i, j;
int [,]dp = new int [N + 1, 3];
for (i = 0; i < N + 1; i++)
{
for (j = 9; j < 3; j ++)
{
dp[i, j] = 0;
}
}
dp[1, 0] = 1;
dp[1, 1] = 1;
dp[1, 2] = 0;
for (i = 2; i <= N; i++)
{
dp[i, 0] = (dp[i - 1, 0] + dp[i - 1, 1] +
dp[i - 1, 2]) % MOD;
dp[i, 1] = dp[i - 1, 0] % MOD;
dp[i, 2] = dp[i - 1, 1] % MOD;
}
int ans = (dp[N, 0] + dp[N, 1] +
dp[N, 2]) % MOD;
return ans;
}
public static void Main (String[] args)
{
int N = 3;
Console.WriteLine(countStrings(N));
}
}
|
Javascript
<script>
var MOD = 1000000007;
function countStrings(N)
{
var i, j;
var dp = Array(N+1).fill(0).map(x => Array(3).fill(0));
for (i = 0; i < N + 1; i++)
{
for (j = 9; j < 3 ; j ++)
{
dp[i][j] = 0;
}
}
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (i = 2; i <= N; i++)
{
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] +
dp[i - 1][2]) % MOD;
dp[i][1] = dp[i - 1][0] % MOD;
dp[i][2] = dp[i - 1][1] % MOD;
}
var ans = (dp[N][0] + dp[N][1] +
dp[N][2]) % MOD;
return ans;
}
var N = 3;
document.write(countStrings(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
13 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...