Check if Binary String Has at Most One Segment of Ones – Java Solution (LeetCode 1784)
Check if Binary String Has at Most One Segment of Ones – Java Solution (LeetCode 1784)

Check if Binary String Has at Most One Segment of Ones – Java Solution (LeetCode 1784)

Learn how to determine whether a binary string contains only one continuous group of 1s. This article explains the problem clearly, builds the intuition step-by-step, and walks through a clean Java solution with time complexity analysis.

19 views
0
0

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:

'0' and '1'

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:

true

If the string contains multiple separated groups of 1s, return:

false


Understanding the Problem Clearly

The key phrase in the problem is:

"at most one contiguous segment of ones"

A segment means a continuous block of characters without interruption.

For example:

111

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

s = "1001"

Structure

1 0 0 1

Here we have:

Segment 1 → "1"
Segment 2 → "1"

There are two separate segments of ones, which violates the condition.

Output

false

Example 2

Input

s = "110"

Structure

1 1 0

There is only one continuous block of ones.

Output

true


Visual Intuition

The string is valid only if it follows this pattern:

111111000000

Meaning:

[One block of 1s] + [any number of 0s]

But the string becomes invalid if we see something like:

111001011

Because here:

1s → stop → 0 → start again → 1

Which means two segments of ones exist.


Key Observation

Since the string starts with 1, the valid structure must look like:

111...111000...000

Once we encounter the first 0, we should never see 1 again.

If we ever see:

0 → followed by → 1

then a new segment of ones has started, which means the answer is false.


Intuition Behind the Solution

The logic becomes very simple:

  1. Traverse the string from left to right.
  2. Keep track of the previous character.
  3. If we ever see the pattern:
0 followed by 1

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

class Solution {
public boolean checkOnesSegment(String s) {
if(s.length() == 1) return true;

if(s.length() == 2 && s.charAt(1) == '1'){
return true;
}

if(s.length() == 2 && s.charAt(1) == '0'){
return true;
}

char prev = '0';

for(int i = 0; i < s.length() - 1; i++){
prev = s.charAt(i);

if(s.charAt(i+1) == '1' && prev == '0'){
return false;
}
}

return true;
}
}


Step-by-Step Code Explanation

1. Handle Small Edge Cases

If the string length is 1:

"1"

There is obviously only one segment, so we return:

true

If the string length is 2, both cases are valid:

"11"
"10"

Because neither creates multiple segments of ones.


2. Traverse the String

We loop through the string:

for(int i = 0; i < s.length() - 1; i++)

At every step, we compare:

current character
next character


3. Detect the Invalid Pattern

We check this condition:

if(s.charAt(i+1) == '1' && prev == '0')

This means we found:

0 → 1

Which indicates a new segment of ones has started, so we return:

false


4. If No Violation is Found

If we finish the loop without encountering the pattern:

0 → 1

then the string contains only one contiguous segment of ones, so we return:

true


Time Complexity

O(n)

Where:

n = length of the string

We traverse the string only once.


Space Complexity

O(1)

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:

"01"

Why?

Because:

111000 → valid

But:

1110011

contains:

01 → followed by another 1

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:

  1. Logical reasoning
  2. Edge case handling
  3. String traversal techniques

Mastering such problems helps build a strong foundation for more complex string and pattern-matching algorithms.

Ai Assistant Kas