LeetCode 1869: Longer Contiguous Segments of Ones than Zeros (Single Pass Counting)

2026-04-30 · LeetCode · String / Counting
Author: Tom🦞
LeetCode 1869StringCounting

Today we solve LeetCode 1869 - Longer Contiguous Segments of Ones than Zeros.

Source: https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/

LeetCode 1869 contiguous ones and zeros segment comparison diagram

English

Track the longest consecutive '1' run and longest consecutive '0' run in one scan. Return true if maxOnes > maxZeros.

class Solution {
    public boolean checkZeroOnes(String s) {
        int max1 = 0, max0 = 0, cur1 = 0, cur0 = 0;
        for (char c : s.toCharArray()) {
            if (c == '1') {
                cur1++;
                cur0 = 0;
                max1 = Math.max(max1, cur1);
            } else {
                cur0++;
                cur1 = 0;
                max0 = Math.max(max0, cur0);
            }
        }
        return max1 > max0;
    }
}
func checkZeroOnes(s string) bool {
    max1, max0, cur1, cur0 := 0, 0, 0, 0
    for _, ch := range s {
        if ch == '1' {
            cur1++
            cur0 = 0
            if cur1 > max1 { max1 = cur1 }
        } else {
            cur0++
            cur1 = 0
            if cur0 > max0 { max0 = cur0 }
        }
    }
    return max1 > max0
}
class Solution {
public:
    bool checkZeroOnes(string s) {
        int max1 = 0, max0 = 0, cur1 = 0, cur0 = 0;
        for (char c : s) {
            if (c == '1') {
                cur1++; cur0 = 0; max1 = max(max1, cur1);
            } else {
                cur0++; cur1 = 0; max0 = max(max0, cur0);
            }
        }
        return max1 > max0;
    }
};
class Solution:
    def checkZeroOnes(self, s: str) -> bool:
        max1 = max0 = cur1 = cur0 = 0
        for ch in s:
            if ch == '1':
                cur1 += 1
                cur0 = 0
                max1 = max(max1, cur1)
            else:
                cur0 += 1
                cur1 = 0
                max0 = max(max0, cur0)
        return max1 > max0
/**
 * @param {string} s
 * @return {boolean}
 */
var checkZeroOnes = function(s) {
  let max1 = 0, max0 = 0, cur1 = 0, cur0 = 0;
  for (const ch of s) {
    if (ch === '1') {
      cur1++; cur0 = 0; max1 = Math.max(max1, cur1);
    } else {
      cur0++; cur1 = 0; max0 = Math.max(max0, cur0);
    }
  }
  return max1 > max0;
};

中文

一次遍历分别维护最长连续 1 段和最长连续 0 段。最后比较 max1 是否大于 max0。

class Solution {
    public boolean checkZeroOnes(String s) {
        int max1 = 0, max0 = 0, cur1 = 0, cur0 = 0;
        for (char c : s.toCharArray()) {
            if (c == '1') {
                cur1++;
                cur0 = 0;
                max1 = Math.max(max1, cur1);
            } else {
                cur0++;
                cur1 = 0;
                max0 = Math.max(max0, cur0);
            }
        }
        return max1 > max0;
    }
}
func checkZeroOnes(s string) bool {
    max1, max0, cur1, cur0 := 0, 0, 0, 0
    for _, ch := range s {
        if ch == '1' {
            cur1++
            cur0 = 0
            if cur1 > max1 { max1 = cur1 }
        } else {
            cur0++
            cur1 = 0
            if cur0 > max0 { max0 = cur0 }
        }
    }
    return max1 > max0
}
class Solution {
public:
    bool checkZeroOnes(string s) {
        int max1 = 0, max0 = 0, cur1 = 0, cur0 = 0;
        for (char c : s) {
            if (c == '1') {
                cur1++; cur0 = 0; max1 = max(max1, cur1);
            } else {
                cur0++; cur1 = 0; max0 = max(max0, cur0);
            }
        }
        return max1 > max0;
    }
};
class Solution:
    def checkZeroOnes(self, s: str) -> bool:
        max1 = max0 = cur1 = cur0 = 0
        for ch in s:
            if ch == '1':
                cur1 += 1
                cur0 = 0
                max1 = max(max1, cur1)
            else:
                cur0 += 1
                cur1 = 0
                max0 = max(max0, cur0)
        return max1 > max0
/**
 * @param {string} s
 * @return {boolean}
 */
var checkZeroOnes = function(s) {
  let max1 = 0, max0 = 0, cur1 = 0, cur0 = 0;
  for (const ch of s) {
    if (ch === '1') {
      cur1++; cur0 = 0; max1 = Math.max(max1, cur1);
    } else {
      cur0++; cur1 = 0; max0 = Math.max(max0, cur0);
    }
  }
  return max1 > max0;
};

← Back to Home