Number of binary strings such that there is no substring of length ≥ 3
Last Updated :
01 Sep, 2023
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, 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 ith 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)th position so we take that value.
dp[i][2] = dp[i - 1][1] as first 1 appeared at (i - 1)th position (consecutively) so we take that value directly.
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 Nth position.
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;
}
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
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
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
JavaScript
<script>
// Javascript implementation of the approach
var MOD = 1000000007;
// Function to return the count of
// all possible binary strings
function countStr(N)
{
var dp = Array.from(Array(N+1), ()=> Array(3).fill(0));
// Base cases
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (var 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
var ans = (dp[N][0] + dp[N][1] + dp[N][2]) % MOD;
return ans;
}
// Driver code
var N = 8;
document.write( countStr(N));
// This code is contributed by itsok.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: Space Optimization
In previous approach the current computation is depend upon the previous 3 computation dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2]) So to optimize space we just required previous 3 computation to get the current answer.
Implementation steps:
- Initialize three variables dp0, dp1, and dp2 to 1, 1, and 0, respectively, which represent the number of binary strings that end in 0, end in 1 with the previous digit being 0, and end in 1 with the previous digit being 1.
- Run a loop from i = 2 to i = N, and in each iteration:
- a. Calculate the new value of dp0 as the sum of dp0, dp1, and dp2, modulo MOD.
- b. Update the value of dp2 as the previous value of dp1.
- c. Update the value of dp1 as the previous value of dp0.
- Calculate the answer by adding the values of dp0, dp1, and dp2, modulo MOD, and return it as the output of the function.
Implementation:
C++
#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 dp0 = 1;
long dp1 = 1;
long dp2 = 0;
for (int i = 2; i <= N; i++) {
// dp[i%3][j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
long temp = dp0;
dp0 = (dp0 + dp1 + dp2) % MOD;
// Taking previously calculated value
dp2 = dp1;
dp1 = temp;
}
// Taking all the possible cases that
// can appear at the Nth position
long ans = (dp0 + dp1 + dp2) % MOD;
return ans;
}
// Driver code
int main()
{
long N = 8;
cout << countStr(N);
return 0;
}
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args)
throws java.lang.Exception
{
long N = 8;
System.out.println(countStr(N));
}
// Function to return the count of
// all possible binary strings
public static long countStr(long N)
{
long dp0 = 1;
long dp1 = 1;
long dp2 = 0;
for (int i = 2; i <= N; i++) {
// dp[i%3][j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
long temp = dp0;
dp0 = (dp0 + dp1 + dp2) % 1000000007;
// Taking previously calculated value
dp2 = dp1;
dp1 = temp;
}
// Taking all the possible cases that
// can appear at the Nth position
long ans = (dp0 + dp1 + dp2) % 1000000007;
return ans;
}
}
Python3
MOD = 1000000007
# Function to return the count of all possible binary strings
def countStr(N):
dp0 = 1
dp1 = 1
dp2 = 0
for i in range(2, N + 1):
# dp[i%3][j] is the number of possible strings such that '1' just appeared consecutively j times upto ith index
temp = dp0
dp0 = (dp0 + dp1 + dp2) % MOD
# Taking previously calculated value
dp2 = dp1
dp1 = temp
# Taking all the possible cases that can appear at the Nth position
ans = (dp0 + dp1 + dp2) % MOD
return ans
# Driver code
N = 8
print(countStr(N))
C#
using System;
class GFG {
// Function to return the count of
// all possible binary strings
public static long CountStr(long N)
{
long dp0 = 1;
long dp1 = 1;
long dp2 = 0;
for (int i = 2; i <= N; i++) {
// dp[i%3][j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
long temp = dp0;
dp0 = (dp0 + dp1 + dp2) % 1000000007;
// Taking previously calculated value
dp2 = dp1;
dp1 = temp;
}
// Taking all the possible cases that
// can appear at the Nth position
long ans = (dp0 + dp1 + dp2) % 1000000007;
return ans;
}
// Driver code
public static void Main(string[] args)
{
long N = 8;
Console.WriteLine(CountStr(N));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
const MOD = 1000000007;
// Function to return the count of
// all possible binary strings
function countStr(N) {
let dp0 = 1;
let dp1 = 1;
let dp2 = 0;
for (let i = 2; i <= N; i++) {
// dp[i%3][j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
let temp = dp0;
dp0 = (dp0 + dp1 + dp2) % MOD;
// Taking previously calculated value
dp2 = dp1;
dp1 = temp;
}
// Taking all the possible cases that
// can appear at the Nth position
let ans = (dp0 + dp1 + dp2) % MOD;
return ans;
}
// Driver code
let N = 8;
console.log(countStr(N));
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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"
6 min read
Number of substrings with odd decimal value in a binary string
Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read
Number of sub-sequences of non-zero length of a binary string divisible by 3
Given a binary string S of length N, the task is to find the number of sub-sequences of non-zero length which are divisible by 3. Leading zeros in the sub-sequences are allowed.Examples: Input: S = "1001" Output: 5 "11", "1001", "0", "0" and "00" are the only subsequences divisible by 3.Input: S = "
5 min read
Number of Binary Strings of length N with K adjacent Set Bits
Given n and k . The task is to find the number of binary strings of length n out of 2n such that they satisfy f(bit string) = k. Where, f(x) = Number of times a set bit is adjacent to another set bit in a binary string x.For Example:f(011101101) = 3f(010100000) = 0f(111111111) = 8Examples: Input : n
15+ min read
Number of even substrings in a string of digits
Given a string of digits 0 - 9. The task is to count a number of substrings which when converting into integer form an even number. Examples : Input : str = "1234".Output : 6"2", "4", "12", "34", "234", "1234" are 6 substring which are even.Input : str = "154".Output : 3Input : str = "15".Output : 0
9 min read
Number of substrings with length divisible by the number of 1's in it
Given a binary string S consisting of only 0's and 1's. Count the number of substrings of this string such that the length of the substring is divisible by the number of 1's in the substring. Examples: Input: S = "01010" Output: 10 Input: S = "1111100000" Output: 25 Naive Approach: Iterate through a
15+ 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 in a Binary String that contains more 1s than 0s
Given a binary string s, the task is to calculate the number of such substrings where the count of 1's is strictly greater than the count of 0's. Examples Input: S = "110011"Output: 11Explanation: Substrings in which the count of 1's is strictly greater than the count of 0's are { S[0]}, {S[0], S[1]
15+ 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
Number of substrings divisible by 6 in a string of integers
Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when convert into integer are divisible by 6. Substring does not contain leading zeroes. Examples: Input : s = "606". Output : 5 Substrings "6", "0", "6", "60", "606" are divisible by 6. Input : s = "48
9 min read