Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1869.html
1869. Longer Contiguous Segments of Ones than Zeros
Level
Easy
Description
Given a binary string s
, return true
if the longest contiguous segment of 1
s is strictly longer than the longest contiguous segment of 0
s in s
. Return false
otherwise.
 For example, in
s = "110100010"
the longest contiguous segment of1
s has length2
, and the longest contiguous segment of0
s has length3
.
Note that if there are no 0
s, then the longest contiguous segment of 0
s is considered to have length 0
. The same applies if there are no 1
s.
Example 1:
Input: s = “1101”
Output: true
Explanation:
The longest contiguous segment of 1s has length 2: “1101”
The longest contiguous segment of 0s has length 1: “1101”
The segment of 1s is longer, so return true.
Example 2:
Input: s = “111000”
Output: false
Explanation:
The longest contiguous segment of 1s has length 3: “111000”
The longest contiguous segment of 0s has length 3: “111000”
The segment of 1s is not longer, so return false.
Example 3:
Input: s = “110100010”
Output: false
Explanation:
The longest contiguous segment of 1s has length 2: “110100010”
The longest contiguous segment of 0s has length 3: “110100010”
The segment of 1s is not longer, so return false.
Constraints:
1 <= s.length <= 100
s[i]
is either'0'
or'1'
.
Solution
Loop over s
and calculate the lengths of the longest contiguous segment of 0’s and the longest contiguous segment of 1’s. Then compare the two lengths and return true
if the longest contiguous segment of 0’s is strictly greater, or false
otherwise.

class Solution { public boolean checkZeroOnes(String s) { int length = s.length(); int maxZeros = 0, maxOnes = 0; int zeros = 0, ones = 0; char prev = '2'; for (int i = 0; i < length; i++) { char curr = s.charAt(i); if (curr == '0') { if (curr == prev) { zeros++; maxZeros = Math.max(maxZeros, zeros); } else { zeros = 1; maxZeros = Math.max(maxZeros, zeros); } } else { if (curr == prev) { ones++; maxOnes = Math.max(maxOnes, ones); } else { ones = 1; maxOnes = Math.max(maxOnes, ones); } } prev = curr; } return maxOnes > maxZeros; } }

// OJ: https://leetcode.com/problems/longercontiguoussegmentsofonesthanzeros/ // Time: O(N) // Space: O(1) class Solution { int longest(string s, char c) { int ans = 0; for (int i = 0; i < s.size(); ) { if (s[i] != c) ++i; else { int start = i; while (i < s.size() && s[i] == c) ++i; ans = max(ans, i  start); } } return ans; } public: bool checkZeroOnes(string s) { return longest(s, '0') < longest(s, '1'); } };

class Solution: def checkZeroOnes(self, s: str) > bool: n0 = n1 = 0 t0 = t1 = 0 for c in s: if c == '0': t0 += 1 t1 = 0 else: t0 = 0 t1 += 1 n0 = max(n0, t0) n1 = max(n1, t1) return n1 > n0 ############ # 1869. Longer Contiguous Segments of Ones than Zeros # https://leetcode.com/problems/longercontiguoussegmentsofonesthanzeros/ class Solution: def checkZeroOnes(self, s: str) > bool: ones = zeroes = 0 for k, g in itertools.groupby(s): if k == "1": ones = max(ones, len(list(g))) else: zeroes = max(zeroes, len(list(g))) return ones > zeroes