Longer Contiguous Segments of Ones than Zeros

To solve this problem, you can track the length of the current segment of 1’s and 0’s as well as the maximum length of segments of 1’s and 0’s seen so far. You then compare the maximum lengths to determine the result.

Here is the Python code implementing this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def checkZeroOnes(self, s: str) -> bool:
        max_ones = max_zeros = 0
        curr_ones = curr_zeros = 0

        for c in s:
            if c == '1':
                curr_ones += 1
                curr_zeros = 0
                max_ones = max(max_ones, curr_ones)
            else:  # c == '0'
                curr_zeros += 1
                curr_ones = 0
                max_zeros = max(max_zeros, curr_zeros)

        return max_ones > max_zeros

This solution iterates over each character in the string s. When a ‘1’ is encountered, it increments curr_ones by 1 and resets curr_zeros to 0. It also updates max_ones if curr_ones exceeds the current max_ones. Similarly, when a ‘0’ is encountered, it increments curr_zeros by 1, resets curr_ones to 0, and updates max_zeros if necessary. Finally, it returns whether max_ones is strictly greater than max_zeros.