class Solution:
def checkZeroOnes(self, s: str) -> bool:
max_ones = max_zeros = 0
current = s[0]
count = 1
for i in range(1, len(s)):
if s[i] == current:
count += 1
else:
if current == '1':
max_ones = max(max_ones, count)
else:
max_zeros = max(max_zeros, count)
current = s[i]
count = 1
# Final segment
if current == '1':
max_ones = max(max_ones, count)
else:
max_zeros = max(max_zeros, count)
return max_ones > max_zeros
class Solution {
public:
bool checkZeroOnes(string s) {
int maxOnes = 0, maxZeros = 0;
char current = s[0];
int count = 1;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == current) {
++count;
} else {
if (current == '1')
maxOnes = max(maxOnes, count);
else
maxZeros = max(maxZeros, count);
current = s[i];
count = 1;
}
}
if (current == '1')
maxOnes = max(maxOnes, count);
else
maxZeros = max(maxZeros, count);
return maxOnes > maxZeros;
}
};
class Solution {
public boolean checkZeroOnes(String s) {
int maxOnes = 0, maxZeros = 0;
char current = s.charAt(0);
int count = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == current) {
count++;
} else {
if (current == '1')
maxOnes = Math.max(maxOnes, count);
else
maxZeros = Math.max(maxZeros, count);
current = s.charAt(i);
count = 1;
}
}
if (current == '1')
maxOnes = Math.max(maxOnes, count);
else
maxZeros = Math.max(maxZeros, count);
return maxOnes > maxZeros;
}
}
var checkZeroOnes = function(s) {
let maxOnes = 0, maxZeros = 0;
let current = s[0], count = 1;
for (let i = 1; i < s.length; i++) {
if (s[i] === current) {
count++;
} else {
if (current === '1')
maxOnes = Math.max(maxOnes, count);
else
maxZeros = Math.max(maxZeros, count);
current = s[i];
count = 1;
}
}
if (current === '1')
maxOnes = Math.max(maxOnes, count);
else
maxZeros = Math.max(maxZeros, count);
return maxOnes > maxZeros;
};
s
(a string consisting only of the characters '0' and '1'), determine whether the longest contiguous segment of '1's is strictly longer than the longest contiguous segment of '0's. Return true
if this is the case, and false
otherwise.
max_ones
) and one for '0's (max_zeros
).count
), as well as the current character (current
).count
.max_ones
or max_zeros
) with count
if it's larger, then reset count
to 1 and update current
to the new character.max_ones
and max_zeros
and return whether max_ones
is greater.s = "1101"
:
current = '1'
, count = 1
.s[1] = '1'
(same as current), increment count
to 2.s[2] = '0'
(different), update max_ones = 2
, set current = '0'
, count = 1
.s[3] = '1'
(different), update max_zeros = 1
, set current = '1'
, count = 1
.max_ones = 2
(since last segment is '1's of length 1, which is not greater than previous max).max_ones = 2
, max_zeros = 1
. Since 2 > 1
, return true
.