192. Check if Binary String Has at Most One Segment of Ones

EasyStringString

Given a binary string s without leading zeros, return 'true' if and only if s contains at most one contiguous segment of ones.

A segment of ones is a maximal substring of consecutive '1' characters.

Input: A binary string s (consisting only of '0' and '1', no leading zeros).

Output: Return 'true' if at most one segment of ones, otherwise 'false'.

Examples

Example 1
Input: 1001
Output: false
Explanation: Two segments of ones: '1' and '1' separated by '00'.
Example 2
Input: 110
Output: true
Explanation: Only one segment of ones: '11'.
Example 3
Input: 1
Output: true
Explanation: Single '1' — one segment.

Constraints

Asked by

AmazonGoogle
Solve this problem in the editor →