Skip to main content

1869. Longer Contiguous Segments of Ones than Zeros

Hint

class Solution {
public:
bool checkZeroOnes(string s)
{
int zeroCnt = 1, oneCnt = 1;
int maxZero = 1, maxOne = 1;
if (s.size() == 1) return s[0] == '1' ? true : false;
// s = "1101"
for (int i = 0; i < s.size() - 1; i++)
{
if (s[i] == '1' && s[i] == s[i + 1])
{
maxOne = max(maxOne, ++oneCnt);
}
else
{
oneCnt = 1;
}
if (s[i] == '0' && s[i] == s[i + 1])
{
maxZero = max(maxZero, ++zeroCnt);
}
else
{
zeroCnt = 1;
}
}
return maxOne > maxZero ? true : false;
}
};
  • T: O()O()
  • S: O()O()