Skip to main content
Back to problems
#1784
Easy Algorithms

Check if binary string has at most one segment of ones

String
39.4% acceptance
Feb 25, 2026
376
1008
Given a binary string s without leading zeros, return true if s contains at most one contiguous segment of ones. Otherwise, return false.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn check_ones_segment(s: String) -> bool {
    // After a '0', there should be no '1'
    !s.contains("01")
  }
}