Count strings with consecutive 1’s
Last Updated :
03 Apr, 2023
Given a number n, count number of n length strings with consecutive 1’s in them.
Examples:
Input : n = 2
Output : 1
There are 4 strings of length 2, the
strings are 00, 01, 10 and 11. Only the
string 11 has consecutive 1's.
Input : n = 3
Output : 3
There are 8 strings of length 3, the
strings are 000, 001, 010, 011, 100,
101, 110 and 111. The strings with
consecutive 1's are 011, 110 and 111.
Input : n = 5
Output : 19
Naive Approach
We will follow the following steps-
- we made a variable “ans” which will tell how many strings have 2 consecutive ones
- We will find all strings with 0 and 1 of a given length using the pick and non-pick concept of recursion
- After that, if any string has maximum consecutive ones greater than or equal to 2 then increment “ans” by 1
- Then finally print the value stored in “ans” variable.
C++
#include <iostream>
using namespace std;
void countStrings( int n, int ind,string str, int &ans)
{
if (ind==n){
int count=0;
int temp=0;
for ( int i=0;i<n;i++){
if (str[i]== '1' ){
temp++;
} else {
temp=0;
}
count=max(count,temp);
}
if (count>=2){ans++;}
return ;
}
countStrings(n,ind+1,str+ "0" ,ans);
countStrings(n,ind+1,str+ "1" ,ans);
}
int main()
{
int ans=0;
countStrings(5,0, "" ,ans) ;
cout <<ans<< endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void countStrings( int n, int ind,
String str, int [] ans)
{
if (ind == n) {
int count = 0 ;
int temp = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '1' ) {
temp++;
}
else {
temp = 0 ;
}
count = Math.max(count, temp);
}
if (count >= 2 ) {
ans[ 0 ]++;
}
return ;
}
countStrings(n, ind + 1 , str + "0" , ans);
countStrings(n, ind + 1 , str + "1" , ans);
}
public static void main(String[] args)
{
int [] ans = { 0 };
countStrings( 5 , 0 , "" , ans);
System.out.println(ans[ 0 ]);
}
}
|
Python3
def countStrings(n, ind, s, ans):
if ind = = n:
count = 0
temp = 0
for i in range (n):
if s[i] = = '1' :
temp + = 1
else :
temp = 0
count = max (count, temp)
if count > = 2 :
ans[ 0 ] + = 1
return
countStrings(n, ind + 1 , s + "0" , ans)
countStrings(n, ind + 1 , s + "1" , ans)
if __name__ = = '__main__' :
ans = [ 0 ]
countStrings( 3 , 0 , "", ans)
print (ans[ 0 ])
|
C#
using System;
public class GFG {
public static void CountStrings( int n, int ind,
string str, ref int ans)
{
if (ind == n) {
int count = 0;
int temp = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '1' ) {
temp++;
}
else {
temp = 0;
}
count = Math.Max(count, temp);
}
if (count >= 2) {
ans++;
}
return ;
}
CountStrings(n, ind + 1, str + "0" , ref ans);
CountStrings(n, ind + 1, str + "1" , ref ans);
}
public static void Main()
{
int ans = 0;
CountStrings(5, 0, "" , ref ans);
Console.WriteLine(ans);
}
}
|
Javascript
function countStrings(n, ind, str, ans){
if (ind == n){
let count = 0;
let temp = 0;
for (let i = 0; i < n; i++){
if (str.charAt(i) == '1' ){
temp++;
}
else {
temp = 0;
}
count = Math.max(count, temp);
}
if (count >= 2) {
ans[0]++;
}
return ;
}
countStrings(n, ind + 1, str + "0" , ans);
countStrings(n, ind + 1, str + "1" , ans);
}
let ans = [0];
countStrings(5, 0, "" , ans);
console.log(ans[0]);
|
Time Complexity: O((2^n) * n), “2^n” for generating all strings, and “n” for traversing each string to count the maximum number of consecutive ones.
Auxiliary Space: O(n),Recursion Stack Space
Optimized Approach
The reverse problem of counting strings without consecutive 1’s can be solved using Dynamic Programming (See the solution here). We can use that solution and find the required count using below steps.
- Compute the number of binary strings without consecutive 1’s using the approach discussed here. Let this count be c.
- Count of all possible binary strings of length n is 2^n.
- Total binary strings with consecutive 1 is 2^n – c.
Below is the implementation of the above steps.
C++
#include <iostream>
using namespace std;
int countStrings( int n)
{
int a[n], b[n];
a[0] = b[0] = 1;
for ( int i = 1; i < n; i++)
{
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
return (1 << n) - a[n - 1] - b[n - 1];
}
int main()
{
cout << countStrings(5) << endl;
return 0;
}
|
Java
class GFG {
static int countStrings( int n)
{
int a[] = new int [n], b[] = new int [n];
a[ 0 ] = b[ 0 ] = 1 ;
for ( int i = 1 ; i < n; i++) {
a[i] = a[i - 1 ] + b[i - 1 ];
b[i] = a[i - 1 ];
}
from 2 ^ n return ( 1 << n) - a[n - 1 ] - b[n - 1 ];
}
public static void main(String args[])
{
System.out.println(countStrings( 5 ));
}
}
|
Python 3
def countStrings(n):
a = [ 0 ] * n
b = [ 0 ] * n
a[ 0 ] = b[ 0 ] = 1
for i in range ( 1 , n):
a[i] = a[i - 1 ] + b[i - 1 ]
b[i] = a[i - 1 ]
return ( 1 << n) - a[n - 1 ] - b[n - 1 ]
print (countStrings( 5 ))
|
C#
using System;
class GFG {
static int countStrings( int n)
{
int [] a = new int [n];
int [] b = new int [n];
a[0] = b[0] = 1;
for ( int i = 1; i < n; i++)
{
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
return (1 << n) - a[n - 1] - b[n - 1];
}
public static void Main()
{
Console.WriteLine(countStrings(5));
}
}
|
PHP
<?php
function countStrings( $n )
{
$a [ $n ] = 0;
$b [ $n ] = 0;
$a [0] = $b [0] = 1;
for ( $i = 1; $i < $n ; $i ++)
{
$a [ $i ] = $a [ $i - 1] + $b [ $i - 1];
$b [ $i ] = $a [ $i - 1];
}
return (1 << $n ) - $a [ $n - 1] -
$b [ $n - 1];
}
echo countStrings(5), "\n" ;
?>
|
Javascript
<script>
function countStrings(n)
{
let a = [], b = [];
a[0] = b[0] = 1;
for (let i = 1; i < n; i++) {
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
return (1 << n) - a[n - 1] - b[n - 1];
}
document.write(countStrings(5));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Optimization:
The time complexity of the above solution is O(n). We can optimize the above solution to work in O(Logn).
If we take a closer look at the pattern of counting strings without consecutive 1’s, we can observe that the count is actually (n+2)th Fibonacci number for n >= 1. The Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ….
n = 1, count = 0 = 21 - fib(3)
n = 2, count = 1 = 22 - fib(4)
n = 3, count = 3 = 23 - fib(5)
n = 4, count = 8 = 24 - fib(6)
n = 5, count = 19 = 25 - fib(7)
................
We can find n’th Fibonacci Number in O(Log n) time (See method 4 here).
Share your thoughts in the comments
Please Login to comment...