VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-strings-with-consecutive-1s/

⇱ Count strings with consecutive 1's - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count strings with consecutive 1's

Last Updated : 9 Jul, 2025

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.

[Naive Approach] Using Recursion - O(2n) Time and O(n) Space

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.


Output
3

[Better Approach 1] Top-Down Dynamic Programming (Memoization)

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'.


Output
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.

[Better Approach 2] Bottom-Up Dynamic Programming - O(n) Time and O(n) Space

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:

  • i is the current position in the string (0-based index),
  • prev is the last bit placed (0 or 1),
  • dp[i][prev] stores the number of valid binary strings from index i to n - 1 starting with the previous bit prev and containing at least one occurrence of consecutive '1's.

Transitions:

  • If prev == 0, then we can place either 0 or 1 freely since no consecutive 1s are formed yet. So, dp[i][0] = dp[i+1][0] + dp[i+1][1]
  • If prev == 1:
    -> Placing 0: we continue as before → dp[i+1][0]
    -> Placing 1: we form a consecutive pair of 1s, and the rest n-i bits can be anything → contributes 2^(n - i)
    -> So, dp[i][1] = dp[i+1][0] + 2^(n - i)

Output
3

[Expected Approach 1] Space-Optimized Bottom-Up Dynamic Programming - O(n) Time and O(1) Space

We iterate from position n to 1, 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:

  • prev0 = count when the previous bit is 0
  • prev1 = count when the previous bit is 1

Then at each position i from n to 1:

  • If previous bit is 0, we can place either 0 or 1, so curr0 = prev0 + prev1
  • If previous bit is 1:
    -> We can place 0 (continue safely)
    -> Or place 1 (forms a valid "11", then rest bits can be anything)curr1 = prev0 + 2^(n - i)

Finally, we return the value of prev0 after the loop since we begin with previous bit as 0.


Output
3

[Expected Approach 2] Complement Counting using Dynamic Programming - O(n) Time and O(1) Space

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:

  • curr0: number of valid binary strings of length i ending in '0' without consecutive 1s.
  • curr1: number of valid binary strings of length i ending in '1' without consecutive 1s.
  • Transition for curr0:
    -> A '0' can be added after both '0' and '1' (no restriction).
    -> So, curr0 = prev0 + prev1
  • Transition for curr1:
    -> A '1' can only be added after a '0' (to avoid consecutive 1s).
    -> So, curr1 = prev0
  • Calculation:
    -> Total number of binary string = 2n
    -> number of strings without consecutive 1s: noConsec = prev0 + prev1
    -> number of strings with at least one pair of consecutive 1s: consec = total - noConsec

Output
3

[Optimized Approach] Using nth Fibonacci - O(log(n)) Time and O(log(n)) Space

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 n can 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:

  • n = 1 → total = 2 → without = fib(3) = 2 → result = 0
  • n = 2 → total = 4 → without = fib(4) = 3 → result = 1
  • n = 3 → total = 8 → without = fib(5) = 5 → result = 3
  • n = 4 → total = 16 → without = fib(6) = 8 → result = 8
  • n = 5 → total = 32 → without = fib(7) = 13 → result = 19

Output
3
Comment
Article Tags: