Skip to content
← All posts

Longer Contiguous Segments of Ones than Zeros

5 min read
leetcodeproblemeasystrings

LeetCode Longer Contiguous Segments of Ones than Zeros (problem 1869) gives you a binary string s. Return true if the longest contiguous segment of '1's is strictly longer than the longest contiguous segment of '0's, or false otherwise.

For example, given s = "1101", the longest run of 1s is 2 (the "11" at the start) and the longest run of 0s is 1 (the single "0" at index 2). Since 2 is strictly greater than 1, the answer is true.

10110213longest 1s = 2longest 0s = 1
String "1101": the longest run of 1s is 2 and the longest run of 0s is 1. Since 2 > 1, return true.

Why this problem matters

This problem is a clean exercise in the "running counter" pattern applied twice in a single pass. Instead of tracking just one streak, you maintain two separate maximums: one for 1s and one for 0s. It is a natural extension of the classic Max Consecutive Ones problem, and it reinforces the idea that you can track multiple pieces of state in a single scan without needing extra data structures.

The pattern of walking through a sequence, counting consecutive runs, and resetting when the character changes is a building block you will reuse in dozens of problems. Getting comfortable with it on an easy problem like this makes harder variations feel familiar.

The approach

Walk through the string character by character. Keep a variable cur_len that counts how long the current run is. At each position:

  1. If the current character matches the previous one, increment cur_len.
  2. If it differs (or it is the first character), reset cur_len to 1.
  3. After updating cur_len, check whether the current character is '1' or '0' and update the corresponding maximum (max_ones or max_zeros).

When you reach the end, compare max_ones and max_zeros. Return true if max_ones is strictly greater.

The solution

def checkZeroOnes(s):
    max_ones = 0
    max_zeros = 0
    cur_len = 0
    for i in range(len(s)):
        if i > 0 and s[i] == s[i - 1]:
            cur_len += 1
        else:
            cur_len = 1
        if s[i] == "1":
            max_ones = max(max_ones, cur_len)
        else:
            max_zeros = max(max_zeros, cur_len)
    return max_ones > max_zeros

Step-by-step walkthrough

Trace through s = "1101". Watch how cur_len grows while the character stays the same and resets to 1 whenever it changes. The two maximums, max_ones and max_zeros, update independently depending on which digit is in the current run.

Step 1: Initialize

Set max_ones = 0, max_zeros = 0, cur_len = 0. We have not scanned anything yet.

10110213
cur_len = 0max_ones = 0max_zeros = 0

Step 2: Index 0, s[0] = '1'

First character. Set cur_len to 1. Since it is a '1', update max_ones to 1.

10110213
cur_len = 1max_ones = 1max_zeros = 0

Step 3: Index 1, s[1] = '1'

Same as previous character. Increment cur_len to 2. Update max_ones to 2.

10110213
cur_len = 2max_ones = 2max_zeros = 0

Step 4: Index 2, s[2] = '0'

Different character. Reset cur_len to 1. Since it is a '0', update max_zeros to 1.

10110213
cur_len = 1max_ones = 2max_zeros = 1

Step 5: Index 3, s[3] = '1'

Different character again. Reset cur_len to 1. max_ones is already 2, so no update.

10110213
cur_len = 1max_ones = 2max_zeros = 1
Result: max_ones (2) > max_zeros (1), return True

The key moment is index 2. That is where the character switches from '1' to '0', resetting cur_len to 1 and recording the first (and only) zero-run. By the end, max_ones = 2 and max_zeros = 1, so the answer is true.

Complexity analysis

ApproachTimeSpace
Single pass with countersO(n)O(1)

Every character is visited exactly once. You only need three integer variables regardless of input size. There is no way to do better than O(n) because you must read every character at least once.

The building blocks

Running counter with reset

The core mechanic is a counter that increments when the current character matches the previous one and resets to 1 otherwise. This generalizes to any "longest run" problem:

cur_len = 0
max_len = 0
for i in range(len(s)):
    if i > 0 and s[i] == s[i - 1]:
        cur_len += 1
    else:
        cur_len = 1
    max_len = max(max_len, cur_len)

Swap in your own condition and the structure stays identical. The twist in this problem is that you split the max-tracking into two buckets based on which character you are counting.

Dual max tracking

Instead of a single max_len, you keep max_ones and max_zeros. This "multi-bucket" idea shows up whenever a problem asks you to compare properties of different categories in a single scan. You will see similar patterns in problems that ask you to track the longest increasing vs. decreasing subarray, or the max frequency of different characters.

When a problem asks you to compare two properties of a sequence, think about whether you can track both in a single pass instead of making two separate passes. It keeps the code simpler and the time complexity the same.

Edge cases

  • All ones: s = "1111". max_ones = 4, max_zeros = 0. Return true because 4 is strictly greater than 0.
  • All zeros: s = "0000". max_ones = 0, max_zeros = 4. Return false.
  • Equal longest runs: s = "10". max_ones = 1, max_zeros = 1. Return false because the problem requires strictly longer, not equal.
  • Single character: s = "1" returns true. s = "0" returns false.
  • Alternating: s = "010101". Both longest runs are 1. Return false.

From understanding to recall

This problem feels almost trivial once you see the solution. The danger is skipping practice because the logic is simple. But in an interview, even easy problems can trip you up under pressure. Do you reset cur_len to 1 or to 0? Do you update the max inside the loop or after it? Do you use > or >= for the final comparison?

Write the solution from scratch on a blank screen. Trace through a few examples by hand. Come back in a few days and do it again. The goal is not to memorize the code but to make the dual-counter pattern feel automatic so you can apply it without hesitation when the problem is harder.

Related posts

Spaced repetition is the most effective way to move patterns like this from short-term understanding to long-term recall. Instead of re-reading solutions the night before an interview, practice retrieving them at increasing intervals. Each retrieval strengthens the connection between the problem shape and the solution pattern, so when you see a variation in a real interview, the approach comes to mind immediately.