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
- 1 <= s.length <= 100
- s[i] is either '0' or '1'.
- s[0] is '1'.
Starter code (python)
class Solution(object):
def checkOnesSegment(self, s):
"""
:type s: str
:rtype: bool
"""
Related problems
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
- Roman to Integer (Easy)
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.