2026-03-06 19:47:33 +0300 MSK

Check if Binary String Has at Most One Segment of Ones

Code

class Solution:
    def checkOnesSegment(self, s: str) -> bool:
        found_1, cur_1 = False, False
        for char in s:
            if char == "1":
                if found_1 and not cur_1:
                    return False
                elif not found_1:
                    found_1 = True
                    cur_1 = True
            elif cur_1:
                cur_1 = False
        return True