A building's security office places a motion sensor in a long corridor overnight. Every minute the sensor logs a 1 if it detected movement during that minute or a 0 if the corridor was empty, producing one long binary string for the whole night. A single visitor walking through produces one uninterrupted burst of 1s, and the security office is comfortable with that pattern. If the log shows two or more separate bursts of 1s -- each burst separated from the next by at least one 0 -- that suggests multiple, possibly unrelated movement events and should be flagged for review.
Given the night's log, determine whether it contains at most one contiguous burst of 1 characters.
A single line containing the binary string s, made up only of the characters 0 and 1.
Print true if s contains at most one contiguous block of 1s (including the case of no 1s at all), otherwise print false.
1 <= length of s <= 100000 s consists only of the characters '0' and '1'.
Example 1
Input
0110
Expected
true
Explanation
The log has exactly one burst of activity, the "11" in the middle, with idle minutes before and after -- a single visitor's pass-through -- so the answer is true.
Example 2
Input
0110110
Expected
false
Explanation
The log shows two separate bursts of `1`s (an "11", then after an idle "0", another "11"), meaning two distinct movement events occurred, so the answer is false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →