Date: 2026-06-06
Time: 17:25
longer-contiguous-segments-of-ones-than-zeros/solution.pySolves LeetCode 1869: given a binary string, determine whether the longest contiguous run of '1's is strictly longer than the longest contiguous run of '0's. This is an Easy-tier string problem that exercises single-pass run-length tracking.
checkZeroOnes(s: str) -> bool — The sole function. Takes a binary string and returns True iff the maximum contiguous segment of '1's exceeds the maximum contiguous segment of '0's. The comparison is strict (>), so equal lengths return False.
The function walks the string character by character, maintaining three pieces of state:
1. cur — length of the current contiguous run
2. prev — the character of the current run (starts as "", so the first character always triggers a reset)
3. maxones / maxzeros — best run lengths seen so far for each digit
On each character:
prev, extend the run (cur += 1)cur = 1, update prev)'1' or '0'This is a single-pass O(n) time, O(1) space algorithm. No intermediate data structures are allocated.
itertools.groupby), it tracks run length inline, which is both simpler and avoids allocation.prev = "" guarantees the first character always enters the else branch, correctly initializing cur = 1 without a special case outside the loop.Imports: None. Pure Python, no standard library usage.
Imported by: The test_solution.py in its own directory. The massive "Imported By" list in the prompt is an artifact of the test harness — those are unrelated test files that share a common test runner infrastructure, not actual consumers of checkZeroOnes.
s is assumed to be non-empty and consist only of '0' and '1' characters (per the LeetCode constraint). No validation is performed.maxones and maxzeros reflect the true maximums. If s is empty, both remain 0 and the function returns False — which is a reasonable degenerate case, though the problem guarantees len(s) >= 1.maxones/maxzeros happens on every iteration (not just at run boundaries), so the final run is always captured without a post-loop fixup.None. The function trusts its input per the LeetCode contract. Passing non-binary characters would silently be counted toward max_zeros (since they aren't "1").