Check if Binary String Has at Most One Segment of Ones
|
|
In this code:
- We first find the index of the first ‘0’ in the string.
- If there’s no ‘0’ in the string, then the entire string is a contiguous segment of ones, and we return
True
. - If there are any ‘1’s in the string after the first ‘0’, then there’s more than one contiguous segment of ones, and we return
False
. - If there are no ‘1’s after the first ‘0’, then there’s only one contiguous segment of ones, and we return
True
.