You are given a binary string s
that consists only of zeros and ones. Return true
if the longest contiguous segment of ones is strictly longer than the longest contiguous segment of zeros in the binary string. Return false
otherwise.
s
?
max_ones
to track the maximum length of contiguous ones segment.max_zeros
to track the maximum length of contiguous zeros segment.current_ones
and current_zeros
to track the current length of contiguous segments while traversing the string.current_ones
and reset current_zeros
to zero.current_zeros
and reset current_ones
to zero.max_ones
and max_zeros
accordingly.max_ones
and max_zeros
:
True
if max_ones
is greater than max_zeros
, otherwise return False
.class Solution:
def checkZeroOnes(self, s: str) -> bool:
max_ones = max_zeros = 0
current_ones = current_zeros = 0
for char in s:
if char == '1':
current_ones += 1
current_zeros = 0
else:
current_zeros += 1
current_ones = 0
max_ones = max(max_ones, current_ones)
max_zeros = max(max_zeros, current_zeros)
return max_ones > max_zeros
By following these steps, the solution efficiently determines whether the longest contiguous segment of ones is strictly longer than the longest contiguous segment of zeros.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?