Try the Question
Before reading the solution, try solving the problem yourself:
👉 https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/
Attempting the problem first helps build your problem-solving intuition, which is essential for coding interviews.
Problem Description
You are given a binary string s, which means the string contains only two characters:
The string does not contain leading zeros, meaning the first character is always 1.
Your task is to determine whether the string contains at most one contiguous segment of 1s.
If the string has only one continuous group of 1s, return:
If the string contains multiple separated groups of 1s, return:
Understanding the Problem Clearly
The key phrase in the problem is:
A segment means a continuous block of characters without interruption.
For example:
This is one segment of ones.
But if 1s are separated by 0s and appear again later, then there are multiple segments.
Example Walkthrough
Example 1
Input
Structure
Here we have:
There are two separate segments of ones, which violates the condition.
Output
Example 2
Input
Structure
There is only one continuous block of ones.
Output
Visual Intuition
The string is valid only if it follows this pattern:
Meaning:
But the string becomes invalid if we see something like:
Because here:
Which means two segments of ones exist.
Key Observation
Since the string starts with 1, the valid structure must look like:
Once we encounter the first 0, we should never see 1 again.
If we ever see:
then a new segment of ones has started, which means the answer is false.
Intuition Behind the Solution
The logic becomes very simple:
- Traverse the string from left to right.
- Keep track of the previous character.
- If we ever see the pattern:
then it means a new segment of ones has started, so return false.
If we finish scanning the string without seeing this pattern, the string is valid.
Java Implementation
Step-by-Step Code Explanation
1. Handle Small Edge Cases
If the string length is 1:
There is obviously only one segment, so we return:
If the string length is 2, both cases are valid:
Because neither creates multiple segments of ones.
2. Traverse the String
We loop through the string:
At every step, we compare:
3. Detect the Invalid Pattern
We check this condition:
This means we found:
Which indicates a new segment of ones has started, so we return:
4. If No Violation is Found
If we finish the loop without encountering the pattern:
then the string contains only one contiguous segment of ones, so we return:
Time Complexity
Where:
We traverse the string only once.
Space Complexity
No extra space is used except a few variables.
A Simpler Observation (Bonus Insight)
A simpler trick for this problem is checking if the string contains:
Why?
Because:
But:
contains:
Which means a second segment of ones exists.
Key Takeaways
✔ Binary strings contain only 0 and 1
✔ A segment means a continuous block
✔ Valid strings contain only one block of ones
✔ The invalid pattern is 0 followed by 1
✔ The solution works with one linear scan
Final Thoughts
Although this problem is categorized as easy, it tests an important concept:
Pattern detection while traversing strings.
Problems like this are common in interviews because they evaluate:
- Logical reasoning
- Edge case handling
- String traversal techniques
Mastering such problems helps build a strong foundation for more complex string and pattern-matching algorithms.




