![]() |
VOOZH | about |
Given a positive integer n, count all possible distinct binary strings of length n such that there are no consecutive 1's.
Examples:
Input: n = 3
Output: 5
Explanation: 5 strings are ("000", "001", "010", "100", "101").Input: n = 2
Output: 3
Explanation: 3 strings are ("00", "01", "10").
Table of Content
The idea is to explore two possible choices at each step of building the binary string. At each index, we have two choices: either place a 0 or place a 1.
5
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: Number of ways to make binary string at i'th index depends on the optimal solutions of countStrings(i+1) and countStrings(i+2). By combining these substructures, we can efficiently calculate number of ways to make binary strings with consecutive 1's at index i.
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times. For example, while calculating countStrings(3), countStrings(4) and countStrings(5) are called. countStrings(4) will again call countStrings(5) which will lead to Overlapping Subproblems.
5
The idea is to fill the DP table based on next values. For each index, we can either place 1 or 0. The array is filled in an iterative manner from i = n-1 to i = 0.
5
In previous approach of dynamic programming we have derive the relation between states as given below:
- dp[i] = dp[i+1] + dp[i+2]
We observe that for calculating dp[i] state we only need dp[i+1] and dp[i+2]. There is no need to store all the next states.
5
The recurrence relation to count binary strings of length i is:
- F(i) = F(i-1) + F(i-2)
with bases cases F(1) = 2 and F(2) = 3. This is similar to Fibonacci Numbers.
Matrix exponentiation can be used to solve this problem in O(logn) time. Refer to Matrix Exponentiation for detailed approach.
5
Related article: