![]() |
VOOZH | about |
Given an integer n, find the count the number of binary strings of length n that contain at least one pair of consecutive 1's. A binary string is a sequence made up of only 0's and 1's.
Examples:
Input: n = 2
Output: 1
Explanation: The 4 strings of length 2 are: 00, 01, 10, 11. Only 11 has consecutive 1's.Input: n = 3
Output: 3
Explanation: The 8 strings of length 3 are: 000, 001, 010, 011, 100, 101, 110, 111. Strings with consecutive 1's are: 011, 110, and 111.
Table of Content
The idea is to simulate the generation of all binary strings of length n using recursion while tracking the previous bit.
Whenever we place two consecutive 1s, we know all further completions will also contain such a pair — so we directly add 2^(remaining length) instead of continuing the recursion. This helps us avoid expanding unnecessary branches.
3
We recursively construct all binary strings of length n, keeping track of whether the previous bit was '1'. At each position i, if we place a '1' after another '1', it means the condition is already satisfied. In this case, we can directly add 2^(n - i) to the result — representing all possible combinations for the remaining bits — and terminate this path early.
This approach avoids exploring unnecessary branches and reduces redundant computation using memoization.
The recursion is defined by the state dp[i][prev], where: i is the current index (bit position) and prev indicates whether the previous bit was '1'.
3
Time Complexity: O(n) since there are only 2 choices for prev and n positions, the total number of unique subproblems is 2 * n.
Auxiliary Space: O(n) due to recursion stack in the worst case and the dp table of size 2 * (n+1) used for memoization.
The idea is to simulate the generation of all binary strings of length n and count those that contain at least one pair of consecutive '1's, using dynamic programming.
We define a 2D DP array dp[i][prev], where:
Transitions:
3
We iterate from position
nto1, maintaining two states: one for previous bit being 0 and one for 1.
If the previous bit is 0, both 0 and 1 can be placed.
If the previous bit is 1, placing another 1 forms a valid pair, and the rest of the bits can take any value (2^(n - i))
Transitions:
Then at each position i from n to 1:
Finally, we return the value of prev0 after the loop since we begin with previous bit as 0.
3
The idea behind this approach is to first count all binary strings of length n that do not contain consecutive 1s, and then subtract that count from the total number of binary strings of length n (which is 2^n).
Transitions:
3
The idea is to compute the total number of strings without consecutive 1's and subtract that from the total number of binary strings. The key observation is that the count of binary strings without consecutive 1's of length n follows the (n + 2)th Fibonacci number. This is because each valid string of length
ncan be formed by appending a 0 or 1 to shorter valid strings, similar to Fibonacci recurrence.To optimize the solution, we use matrix exponentiation which allows us to compute the nth Fibonacci number in O(log n) time instead of linear time or dynamic programming.
If we take a closer look at the pattern of counting strings without consecutive 1's, we can observe that the count is actually the (n + 2)th Fibonacci number for n >= 1. The Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
Examples:
3