Count N-length Binary Strings consisting of “11” as substring
Last Updated :
20 Mar, 2023
Given a positive integer N, the task is to find the number of binary strings of length N which contains “11” as a substring.
Examples:
Input: N = 2
Output: 1
Explanation: The only string of length 2 that has “11” as a substring is “11”.
Input: N = 12
Output: 3719
Approach: The idea is to derive the number of possibilities of having “11” as a substring for binary representations starting with 0 or 1 based on the following observations:
- If the first bit is 0, then the starting bit does not contribute to the string having “11” as a substring. Therefore, the remaining (N – 1) bits have to form a string having “11” as a substring.
- If the first bit is 1 and the following bit is also 1, then there exists 2(N – 2) strings having “11” as a substring.
- If the first bit is 1 but the following bit is 0, then a string having “11” as a substring can be formed with remaining (N – 2) bits.
- Therefore, the recurrence relation to generate all the binary strings of length N is:
dp[i] = dp[i – 1] + dp[i – 2] + 2(i – 2)
where,
dp[i] is the string of length i having “11” as a substring.
and dp[0] = dp[1] = 0.
Recursive Solution (Naive Solution):
here we have to find the number of possibilities having “11” as a substring for binary representation starting with 0 or 1 based on the observations. so above mentioned approach is for that. but in a recursive solution if we want to find the solution for i then that should be calculated as follows,
binaryStrings(i) = binaryStrings(i-1) + binaryStrings(i-2) + 2(i-2)
which can be solved recursively as follow.
C++
#include <bits/stdc++.h>
using namespace std;
int binaryStrings( int N) {
if (N == 0 || N == 1) {
return 0;
}
return binaryStrings(N - 1) + binaryStrings(N - 2) + (1 << (N - 2));
}
int main()
{
int N = 12;
cout << binaryStrings(N);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static int BinaryStrings( int N)
{
if (N == 0 || N == 1 ) {
return 0 ;
}
return BinaryStrings(N - 1 ) + BinaryStrings(N - 2 )
+ ( 1
<< (N
- 2 ));
}
public static void main(String[] args)
{
int N = 12 ;
System.out.println(BinaryStrings(N));
}
}
|
Python3
def binaryStrings(N: int ) - > int :
if N = = 0 or N = = 1 :
return 0
return binaryStrings(N - 1 ) + binaryStrings(N - 2 ) + ( 1 << (N - 2 ))
if __name__ = = "__main__" :
N = 12
print (binaryStrings(N))
|
C#
using System;
public class Program {
static int BinaryStrings( int N) {
if (N == 0 || N == 1) {
return 0;
}
return BinaryStrings(N - 1) + BinaryStrings(N - 2) + (1 << (N - 2));
}
public static void Main() {
int N = 12;
Console.WriteLine(BinaryStrings(N));
}
}
|
Javascript
function binaryStrings(N)
{
if (N == 0 || N == 1)
{
return 0;
}
return binaryStrings(N - 1) + binaryStrings(N - 2) + (1 << (N - 2));
}
let N = 12;
console.log(binaryStrings(N));
|
Optimized Solution:
Follow the steps below to solve the problem:
- Initialize an array, say dp[], of size (N + 1) and assign dp[0] as 0 and dp[1] as 0.
- Precompute the first N powers of 2 and store it in an array, say power[].
- Iterate over the range [2, N] and update dp[i] as (dp[i – 1] + dp[i – 2] + power[i – 2]).
- After completing the above steps, print the value of dp[N] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void binaryStrings( int N)
{
int dp[N + 1];
dp[0] = 0;
dp[1] = 0;
for ( int i = 2; i <= N; i++) {
dp[i] = dp[i - 1]
+ dp[i - 2]
+ (1<<(i-2));
}
cout << dp[N];
}
int main()
{
int N = 12;
binaryStrings(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void binaryStrings( int N)
{
int [] dp = new int [N + 1 ];
dp[ 0 ] = 0 ;
dp[ 1 ] = 0 ;
int [] power = new int [N + 1 ];
power[ 0 ] = 1 ;
for ( int i = 1 ; i <= N; i++)
{
power[i] = 2 * power[i - 1 ];
}
for ( int i = 2 ; i <= N; i++)
{
dp[i] = dp[i - 1 ] + dp[i - 2 ] + power[i - 2 ];
}
System.out.println(dp[N]);
}
public static void main(String[] args)
{
int N = 12 ;
binaryStrings(N);
}
}
|
Python3
def binaryStrings(N):
dp = [ 0 ] * (N + 1 )
dp[ 0 ] = 0
dp[ 1 ] = 0
power = [ 0 ] * (N + 1 )
power[ 0 ] = 1
for i in range ( 1 , N + 1 ):
power[i] = 2 * power[i - 1 ]
for i in range ( 2 , N + 1 ):
dp[i] = dp[i - 1 ] + dp[i - 2 ] + power[i - 2 ]
print (dp[N])
if __name__ = = '__main__' :
N = 12
binaryStrings(N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void binaryStrings( int N)
{
int []dp = new int [N + 1];
dp[0] = 0;
dp[1] = 0;
int []power = new int [N + 1];
power[0] = 1;
for ( int i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
}
for ( int i = 2; i <= N; i++) {
dp[i] = dp[i - 1]
+ dp[i - 2]
+ power[i - 2];
}
Console.WriteLine(dp[N]);
}
public static void Main()
{
int N = 12;
binaryStrings(N);
}
}
|
Javascript
<script>
function binaryStrings(N) {
var dp = Array(N + 1).fill(0);
dp[0] = 0;
dp[1] = 0;
var power = Array(N+1).fill(0);
power[0] = 1;
for (i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
}
for (i = 2; i <= N; i++) {
dp[i] = dp[i - 1] + dp[i - 2] + power[i - 2];
}
document.write(dp[N]);
}
var N = 12;
binaryStrings(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...