File: longer-contiguous-segments-of-ones-than-zeros/solution.py

Date: 2026-06-06

Time: 17:25

longer-contiguous-segments-of-ones-than-zeros/solution.py

Purpose

Solves 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.

Key Components

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.

Flow

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:

This is a single-pass O(n) time, O(1) space algorithm. No intermediate data structures are allocated.

Patterns

Dependencies

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.

Invariants

Error Handling

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").