


Can you try to solve this problem in O(N) time and O(1) space?
If ‘N’ = 3
Then there are 5 possible binary strings of length equal to 3 that do not contain consecutive 1’s: “000”, “001”, “010”, “100”, “101”.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’, denoting the length of the binary string(s).
For each test case, print the count of possible binary strings of length equal to ‘N’ that do not contain consecutive 1’s.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ N ≤ 20000
Time limit: 1 sec
There are 2^N binary strings possible of length equal to N, we can recursively generate all possible binary strings, we can even prune this search by using a standard backtracking technique and generate only the strings that satisfy the given condition, but in this approach, we will discuss something that is bottom-up in nature and can later be extended to memoize results and become an efficient dynamic programming solution, but for this solution, we will limit our discussion to just a simple recursion.
For each of the i’th positions, we only have two possible bits to place as this is a binary string. We are allowed to place 1 at the i’th position only when i-1’th position doesn’t contain a 1, in other words, the number of strings that will be formed by placing 1 at the i’th position is equal to the number of strings formed by placing 0 at the i-1’th position. Further, the number of strings formed by placing 0 at the i’th position is equal to the number of strings formed by placing either a 0 or a 1 at the i-1’th position. This creates a perfect recursive approach as we are recalculating things in a similar way for each of the positions in the binary string.
For each of the i’th positions, we only have two possible bits to place as this is a binary string. We are allowed to place 1 at the i’th position only when i-1’th position doesn’t contain a 1, in other words, the number of strings that will be formed by placing 1 at the i’th position is equal to the number of strings formed by placing 0 at the i-1’th position. Further, the number of strings formed by placing 0 at the i’th position is equal to the sum of the number of strings formed by placing either a 0 or a 1 at the i-1’th position. This creates a perfect recursive approach as we are recalculating things in a similar way for each of the positions in the binary string.
But we can avoid the recalculations if we use a dp-table to store the number of strings of length equal to ‘X’ ending with a 0-bit or a 1-bit. This way we won’t have to recalculate things and is a standard way to memoize recursive solutions and convert them into dynamic programming.
For each of the i’th positions, we only have two possible bits to place as this is a binary string. We are allowed to place 1 at the i’th position only when the i-1’th position doesn’t contain a ‘1’, in other words, the number of strings that will be formed by placing 1 at the i’th position is equal to the number of strings formed by placing 0 at the i-1’th position. Further, the number of strings formed by placing 0 at the i’th position is equal to the sum of the number of strings formed by placing either a 0 or a 1 at the i-1’th position.
Let’s define our dp states now, we can define dp[i][j] equal to the count of binary strings that are of length equal to ‘i’ and contain ‘j’ as the last bit. We can define transitions as dp[i][1] = dp[i-1][0] and dp[i][1] = dp[i-1][0] + dp[i-1][1]. A clever observation here is that we need not store all the dp results for ‘i’ equal to 0 to N - 1, rather we just need to store the results of the previous index which can be done with the help of just two variables. This will save a lot of space and convert a linear space solution to a constant space solution.