Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s.
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
This problem can be solved using Dynamic Programming. Let a[i] be the number of binary strings of length i which do not contain any two consecutive 1’s 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 = b = 1. The total number of strings of length i is just a[i] + b[i].
Following is the implementation of above solution. In the following implementation, indexes start from 0. So a[i] represents the number of binary strings for input length i+1. Similarly, b[i] represents binary strings for input length i+1.
If we take a closer look at the pattern, 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 = 2 = fib(3) n = 2, count = 3 = fib(4) n = 3, count = 5 = fib(5) n = 4, count = 8 = fib(6) n = 5, count = 13 = fib(7) ................
Therefore we can count the strings in O(Log n) time also using the method 5 here.
This article is contributed by Rahul Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Count number of binary strings without consecutive 1’s : Set 2
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Strings formed from given characters without any consecutive repeating characters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count of sub-strings with equal consecutive 0's and 1's
- Count of strings possible by replacing two consecutive same character with new character
- Count strings with consecutive 1's
- Minimum time to finish tasks without skipping two consecutive
- Minimize total cost without repeating same task in two consecutive iterations
- Count of binary strings of length N having equal count of 0's and 1's
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Python | Check if there are K consecutive 1's in a binary number
- Maximum number of consecutive 1's in binary representation of all the array elements
- Minimum number of Binary strings to represent a Number
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count the number of ordered sets not containing consecutive numbers