Python Program to Count number of binary strings without consecutive 1’s
Last Updated :
10 Nov, 2023
Write a Python program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s.
Examples:
Input: N = 2
Output: 3
// The 3 strings are 00, 01, 10
Input: N = 3
Output: 5
// The 5 strings are 000, 001, 010, 100, 101
Python Program to Count a number of binary strings without consecutive 1’s using Dynamic Programming:
Let a[i] be the number of binary strings of length i that do not contain any two consecutive 1s and which end in 0. Similarly, let b[i] be the number of such strings which end in 1. We can append either 0 or 1 to a string ending in 0, but we can only append 0 to a string ending in 1. This yields the recurrence relation:
a[i] = a[i – 1] + b[i – 1]
b[i] = a[i – 1]
The base cases of above recurrence are a[1] = b[1] = 1. The total number of strings of length i is just a[i] + b[i].
Below is the implementation of the above approach:
Python3
def countStrings(n):
a = [ 0 for i in range (n)]
b = [ 0 for i in range (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 a[n - 1 ] + b[n - 1 ]
print (countStrings( 3 ))
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Another method:
From the above method it is clear that we only want just previous value in the for loop which we can also do by replacing the array with the variable.
Below is the implementation of the above approach:
Python3
class Subset_sum :
@staticmethod
def countStrings( n) :
a = 1
b = 1
i = 1
while (i < n) :
temp = a + b
b = a
a = temp
i + = 1
return (a + b) % 1000000007
@staticmethod
def main( args) :
print (Subset_sum.countStrings( 3 ))
if __name__ = = "__main__" :
Subset_sum.main([])
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Fibonacci in Log (n) without using Matrix Exponentiation:
As for calculating the n we can express it as the product of n/2 and n/2 ((n/2+1) for n is odd) as they are independent ,now as there are the cases when we combining these two half values to get full length there may be occurrence of consecutive 1’s at the joining. So we have to subtract those cases to get the final result.
f(n)=f(n/2)*f(n/2) – (those values which has consecutive 1’s at the joining part)
Below is the implementation of the above approach:
Python3
class Solution:
def __init__( self ):
self .mp = {}
def countStrings( self , N):
if N = = 0 :
return 1
if N = = 1 :
return 2
if N = = 2 :
return 3
mod = 10 * * 9 + 7
a = self .mp.get(N / / 2 - 1 , self .countStrings(N / / 2 - 1 )) % mod
b = self .mp.get(N / / 2 , self .countStrings(N / / 2 )) % mod
c = self .mp.get(N / / 2 + 1 , self .countStrings(N / / 2 + 1 )) % mod
if N % 2 :
return (b * c - (c - b) * (b - a) + mod) % mod
return (b * b - (b - a) * (b - a) + mod) % mod
t = 1
while t > 0 :
N = 10
obj = Solution()
print (obj.countStrings(N))
t - = 1
|
Time Complexity: O(log N)
Auxiliary Space: O(log N)
Please refer complete article on Count number of binary strings without consecutive 1’s for more details!
Share your thoughts in the comments
Please Login to comment...