11. Trail of Ones
✅ GFG solution to the Trail of Ones problem: count binary strings of length n with at least one pair of consecutive 1's using dynamic programming approach. 🚀
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given an integer n
, the task is to count the number of binary strings of length n
that contains at least one pair of consecutive 1's.
A binary string is a sequence made up of only 0's and 1's. We need to find how many such strings of length n
have at least one occurrence of "11" as a substring.
📘 Examples
Example 1
Input: n = 2
Output: 1
Explanation: There are 4 strings of length 2, the strings are 00, 01, 10, and 11.
Only the string 11 has consecutive 1's.
Example 2
Input: n = 3
Output: 3
Explanation: There are 8 strings of length 3, the strings are 000, 001, 010, 011, 100, 101, 110 and 111.
The strings with consecutive 1's are 011, 110 and 111.
Example 3
Input: n = 5
Output: 19
Explanation: There are 19 strings having at least one pair of consecutive 1's.
🔒 Constraints
$2 \le n \le 30$
✅ My Approach
The optimal approach uses Dynamic Programming with a complementary counting strategy. Instead of directly counting strings with consecutive 1's, we count strings without consecutive 1's and subtract from the total.
Dynamic Programming Approach
Key Insight:
Total binary strings of length n = 2^n
Strings with at least one consecutive 1's = Total - Strings with no consecutive 1's
State Definition:
Let
a
= number of valid strings ending with 0Let
b
= number of valid strings ending with 1
Recurrence Relation:
For strings ending with 0: we can append 0 to any valid string (both ending with 0 or 1)
For strings ending with 1: we can only append 1 to strings ending with 0 (to avoid consecutive 1's)
new_a = a + b
(append 0 to any valid string)new_b = a
(append 1 only to strings ending with 0)
Base Cases:
For n=1: a=1 (string "0"), b=1 (string "1")
Total valid strings without consecutive 1's = a + b
Final Answer:
Answer = 2^n - (a + b)
📝 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the length of the binary string. We iterate through n positions once, performing constant time operations at each step.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for storing the two variables
a
andb
, regardless of the input size.
🧑💻 Code (C++)
class Solution {
public:
int countConsec(int n) {
int a = 0, b = 0;
for (int i = n; i; i--) {
int tmp = a + b;
b = a + (1 << (n - i));
a = tmp;
}
return a;
}
};
🧑💻 Code (Java)
class Solution {
public int countConsec(int n) {
int a = 0, b = 0;
for (int i = n; i > 0; i--) {
int tmp = a + b;
b = a + (1 << (n - i));
a = tmp;
}
return a;
}
}
🐍 Code (Python)
class Solution:
def countConsec(self, n: int) -> int:
a = b = 0
for i in range(n, 0, -1):
a, b = a + b, a + (1 << (n - i))
return a
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let's make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated