Find the number of binary strings of length N with at least 3 consecutive 1s
Last Updated :
31 May, 2021
Given an integer N. The task is to find the number of all possible distinct binary strings of length N which have at least 3 consecutive 1s.
Examples:
Input: N = 3
Output: 1
The only string of length 3 possible is “111”.
Input: N = 4
Output: 3
The 3 strings are “1110”, “0111” and “1111”.
Naive approach: Consider all possible strings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string& s)
{
int n = s.length();
for ( int i = 2; i < n; i++) {
if (s[i] == '1' && s[i - 1] == '1' && s[i - 2] == '1' )
return 1;
}
return 0;
}
int countStr( int i, string& s)
{
if (i < 0) {
if (check(s))
return 1;
return 0;
}
s[i] = '0' ;
int ans = countStr(i - 1, s);
s[i] = '1' ;
ans += countStr(i - 1, s);
s[i] = '0' ;
return ans;
}
int main()
{
int N = 4;
string s(N, '0' );
cout << countStr(N - 1, s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean check(String s)
{
int n = s.length();
for ( int i = 2 ; i < n; i++)
{
if (s.charAt(i) == '1' &&
s.charAt(i- 1 ) == '1' &&
s.charAt(i- 2 ) == '1' )
return true ;
}
return false ;
}
static int countStr( int i, String s)
{
if (i < 0 )
{
if (check(s))
return 1 ;
return 0 ;
}
char [] myNameChars = s.toCharArray();
myNameChars[i] = '0' ;
s = String.valueOf(myNameChars);
int ans = countStr(i - 1 , s);
char [] myChar = s.toCharArray();
myChar[i] = '1' ;
s = String.valueOf(myChar);
ans += countStr(i - 1 , s);
char []myChar1 = s.toCharArray();
myChar1[i] = '0' ;
s = String.valueOf(myChar1);
return ans;
}
public static void main(String args[])
{
int N = 4 ;
String s = "0000" ;
System.out.println(countStr(N - 1 , s));
}
}
|
Python3
def check(s) :
n = len (s);
for i in range ( 2 , n) :
if (s[i] = = '1' and s[i - 1 ] = = '1' and
s[i - 2 ] = = '1' ) :
return 1 ;
def countStr(i, s) :
if (i < 0 ) :
if (check(s)) :
return 1 ;
return 0 ;
s[i] = '0' ;
ans = countStr(i - 1 , s);
s[i] = '1' ;
ans + = countStr(i - 1 , s);
s[i] = '0' ;
return ans;
if __name__ = = "__main__" :
N = 4 ;
s = list ( '0' * N);
print (countStr(N - 1 , s));
|
C#
using System;
class GFG
{
static bool check(String s)
{
int n = s.Length;
for ( int i = 2; i < n; i++)
{
if (s[i] == '1' && s[i - 1] == '1' && s[i - 2] == '1' )
return true ;
}
return false ;
}
static int countStr( int i, String s)
{
if (i < 0)
{
if (check(s))
return 1;
return 0;
}
char [] myNameChars = s.ToCharArray();
myNameChars[i] = '0' ;
s = String.Join( "" , myNameChars);
int ans = countStr(i - 1, s);
char [] myChar = s.ToCharArray();
myChar[i] = '1' ;
s = String.Join( "" , myChar);
ans += countStr(i - 1, s);
char []myChar1 = s.ToCharArray();
myChar1[i] = '0' ;
s = String.Join( "" , myChar1);
return ans;
}
public static void Main(String []args)
{
int N = 4;
String s = "0000" ;
Console.WriteLine(countStr(N - 1, s));
}
}
|
Javascript
<script>
function check(s)
{
let n = s.length;
for (let i = 2; i < n; i++)
{
if (s[i] == '1 ' &&
s[i-1] == ' 1 ' &&
s[i-2] == ' 1 ')
return true;
}
return false;
}
// Function to return the count
// of required strings
function countStr(i,s)
{
if (i < 0)
{
if (check(s))
return 1;
return 0;
}
let myNameChars = s.split("");
myNameChars[i] = ' 0 ';
s = (myNameChars).join("");
let ans = countStr(i - 1, s);
let myChar = s.split("");
myChar[i] = ' 1 ';
s = (myChar).join("");
ans += countStr(i - 1, s);
let myChar1 = s.split("");
myChar1[i] = ' 0';
s = (myChar1).join( "" );
return ans;
}
let N = 4;
let s = "0000" ;
document.write(countStr(N - 1, s));
</script>
|
Time Complexity: O(2N)
Space Complexity: O(N) because of recurrence stack space.
Efficient approach: We use dynamic programming for computing the number of strings.
State of dp: dp(i, x) : denotes number of strings of length i with x consecutive 1s in position i + 1 to i + x.
Recurrence: dp(i, x) = dp(i – 1, 0) + dp(i – 1, x + 1)
The recurrence is based on the fact that either the string can have a ‘0’ at position i or a ‘1’.
- If it has a ‘0’ at position i then for (i-1)th position value of x = 0.
- If it has a ‘1’ at position i the for (i-1)th position value of x = value of x at position i + 1.
Base Condition: dp(i, 3) = 2i. Because once you have 3 consecutive ‘1’s you don’t care what characters are there at position 1, 2…i of string as all the strings are valid.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int n;
int solve( int i, int x, int dp[][4])
{
if (i < 0)
return x == 3;
if (dp[i][x] != -1)
return dp[i][x];
dp[i][x] = solve(i - 1, 0, dp);
dp[i][x] += solve(i - 1, x + 1, dp);
return dp[i][x];
}
int main()
{
n = 4;
int dp[n][4];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < 4; j++)
dp[i][j] = -1;
for ( int i = 0; i < n; i++) {
dp[i][3] = (1 << (i + 1));
}
cout << solve(n - 1, 0, dp);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int n;
static int solve( int i, int x, int dp[][])
{
if (i < 0 )
{
return x == 3 ? 1 : 0 ;
}
if (dp[i][x] != - 1 )
{
return dp[i][x];
}
dp[i][x] = solve(i - 1 , 0 , dp);
dp[i][x] += solve(i - 1 , x + 1 , dp);
return dp[i][x];
}
public static void main(String[] args)
{
n = 4 ;
int dp[][] = new int [n][ 4 ];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < 4 ; j++)
{
dp[i][j] = - 1 ;
}
}
for ( int i = 0 ; i < n; i++)
{
dp[i][ 3 ] = ( 1 << (i + 1 ));
}
System.out.print(solve(n - 1 , 0 , dp));
}
}
|
Python 3
def solve(i, x, dp):
if (i < 0 ):
return x = = 3
if (dp[i][x] ! = - 1 ):
return dp[i][x]
dp[i][x] = solve(i - 1 , 0 , dp)
dp[i][x] + = solve(i - 1 , x + 1 , dp)
return dp[i][x]
if __name__ = = "__main__" :
n = 4 ;
dp = [[ 0 for i in range (n)] for j in range ( 4 )]
for i in range (n):
for j in range ( 4 ):
dp[i][j] = - 1
for i in range (n) :
dp[i][ 3 ] = ( 1 << (i + 1 ))
print (solve(n - 1 , 0 , dp))
|
C#
using System;
class GFG
{
static int n;
static int solve( int i, int x, int [,]dp)
{
if (i < 0)
{
return x == 3 ? 1 : 0;
}
if (dp[i,x] != -1)
{
return dp[i,x];
}
dp[i,x] = solve(i - 1, 0, dp);
dp[i,x] += solve(i - 1, x + 1, dp);
return dp[i,x];
}
public static void Main(String[] args)
{
n = 4;
int [,]dp = new int [n, 4];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < 4; j++)
{
dp[i, j] = -1;
}
}
for ( int i = 0; i < n; i++)
{
dp[i,3] = (1 << (i + 1));
}
Console.Write(solve(n - 1, 0, dp));
}
}
|
Javascript
<script>
var n;
function solve(i , x , dp)
{
if (i < 0) {
return x == 3 ? 1 : 0;
}
if (dp[i][x] != -1) {
return dp[i][x];
}
dp[i][x] = solve(i - 1, 0, dp);
dp[i][x] += solve(i - 1, x + 1, dp);
return dp[i][x];
}
n = 4;
var dp = Array(n).fill().map(()=>Array(4).fill(0));
for (i = 0; i < n; i++) {
for (j = 0; j < 4; j++) {
dp[i][j] = -1;
}
}
for (i = 0; i < n; i++) {
dp[i][3] = (1 << (i + 1));
}
document.write(solve(n - 1, 0, dp));
</script>
|
Time complexity: O(N)
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...