Longer Contiguous Segments of Ones than Zeros - Problem
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.
Input & Output
Example 1 — Mixed Segments
$
Input:
s = "110100010"
›
Output:
false
💡 Note:
Longest segment of 1s has length 2 (positions 0-1), longest segment of 0s has length 3 (positions 4-6). Since 2 ≤ 3, return false.
Example 2 — 1s Win
$
Input:
s = "1110001"
›
Output:
true
💡 Note:
Longest segment of 1s has length 3 (positions 0-2), longest segment of 0s has length 3 (positions 3-5). But wait - there's also a single 1 at the end, so we have segments [3,1] for 1s and [3] for 0s. Max 1s = 3, Max 0s = 3. Actually this should be false. Let me recalculate: segments are 111|000|1, so max 1s = 3, max 0s = 3, result is false.
Example 3 — All Ones
$
Input:
s = "111"
›
Output:
true
💡 Note:
Only 1s present with length 3, no 0s (length 0). Since 3 > 0, return true.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists only of '0' and '1'
Visualization
Tap to expand
Understanding the Visualization
1
Input
Binary string with mixed segments of 0s and 1s
2
Process
Find longest contiguous segment for each digit type
3
Output
Return true if max 1s segment > max 0s segment
Key Takeaway
🎯 Key Insight: Track maximum segment lengths for both digit types in a single pass through the string
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code