Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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, or return false otherwise.
s = "110100010" the longest continuous segment of 1s has length 2, and the longest continuous segment of 0s has length 3.Note that if there are no 0's, then the longest continuous segment of 0's is considered to have a length 0. The same applies if there is 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 <= 100s[i] is either '0' or '1'.Problem summary: 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, or return false otherwise. For example, in s = "110100010" the longest continuous segment of 1s has length 2, and the longest continuous segment of 0s has length 3. Note that if there are no 0's, then the longest continuous segment of 0's is considered to have a length 0. The same applies if there is no 1's.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"1101"
"111000"
"110100010"
max-consecutive-ones)count-subarrays-with-more-ones-than-zeros)check-if-binary-string-has-at-most-one-segment-of-ones)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1869: Longer Contiguous Segments of Ones than Zeros
class Solution {
public boolean checkZeroOnes(String s) {
return f(s, '1') > f(s, '0');
}
private int f(String s, char x) {
int cnt = 0, mx = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == x) {
mx = Math.max(mx, ++cnt);
} else {
cnt = 0;
}
}
return mx;
}
}
// Accepted solution for LeetCode #1869: Longer Contiguous Segments of Ones than Zeros
func checkZeroOnes(s string) bool {
f := func(x rune) int {
cnt, mx := 0, 0
for _, c := range s {
if c == x {
cnt++
mx = max(mx, cnt)
} else {
cnt = 0
}
}
return mx
}
return f('1') > f('0')
}
# Accepted solution for LeetCode #1869: Longer Contiguous Segments of Ones than Zeros
class Solution:
def checkZeroOnes(self, s: str) -> bool:
def f(x: str) -> int:
cnt = mx = 0
for c in s:
if c == x:
cnt += 1
mx = max(mx, cnt)
else:
cnt = 0
return mx
return f("1") > f("0")
// Accepted solution for LeetCode #1869: Longer Contiguous Segments of Ones than Zeros
struct Solution;
impl Solution {
fn check_zero_ones(s: String) -> bool {
let s: Vec<char> = s.chars().collect();
let n = s.len();
let mut state = '2';
let mut length = 0;
let mut zero = 0;
let mut one = 0;
for i in 0..n {
if s[i] == state {
length += 1;
} else {
state = s[i];
length = 1;
}
if s[i] == '0' {
zero = zero.max(length);
} else {
one = one.max(length);
}
}
one > zero
}
}
#[test]
fn test() {
let s = "1101".to_string();
let res = true;
assert_eq!(Solution::check_zero_ones(s), res);
let s = "111000".to_string();
let res = false;
assert_eq!(Solution::check_zero_ones(s), res);
let s = "110100010".to_string();
let res = false;
assert_eq!(Solution::check_zero_ones(s), res);
}
// Accepted solution for LeetCode #1869: Longer Contiguous Segments of Ones than Zeros
function checkZeroOnes(s: string): boolean {
const f = (x: string): number => {
let [mx, cnt] = [0, 0];
for (const c of s) {
if (c === x) {
mx = Math.max(mx, ++cnt);
} else {
cnt = 0;
}
}
return mx;
};
return f('1') > f('0');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.