Zepruv › Coding problems

Check if Binary String Has at Most One Segment of Ones

Easy · String

Given a binary string s ​​​​​without leading zeros, return true​​​ if s contains at most one contiguous segment of ones. Otherwise, return false.

Example 1

Input: s = "1001"
Output: false

The ones do not form a contiguous segment.

Example 2

Input: s = "110"
Output: true

Constraints

Starter code (python)

class Solution(object):
    def checkOnesSegment(self, s):
        """
        :type s: str
        :rtype: bool
        """
        

Related problems

Previous: Second Largest Digit in a String · Next: Minimum Elements to Add to Form a Given Sum · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.