Longer Contiguous Segments of Ones than Zeros - Problem

Given a binary string s, determine whether the longest contiguous segment of 1's is strictly longer than the longest contiguous segment of 0's.

A contiguous segment is a sequence of consecutive identical characters. For example:

  • In "110100010", the longest segment of 1's has length 2 ("11")
  • The longest segment of 0's has length 3 ("000")
  • Since 2 < 3, we return false

Special cases:

  • If there are no 0's in the string, the longest segment of 0's is considered to have length 0
  • If there are no 1's in the string, the longest segment of 1's is considered to have length 0

Return true if the longest segment of 1's is strictly longer than the longest segment of 0's, otherwise return false.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "1101"
โ€บ Output: true
๐Ÿ’ก Note: The longest segment of 1's is "11" with length 2. The longest segment of 0's is "0" with length 1. Since 2 > 1, return true.
example_2.py โ€” Longer Zeros
$ Input: s = "110100010"
โ€บ Output: false
๐Ÿ’ก Note: The longest segment of 1's is "11" with length 2. The longest segment of 0's is "000" with length 3. Since 2 < 3, return false.
example_3.py โ€” Only Ones
$ Input: s = "111"
โ€บ Output: true
๐Ÿ’ก Note: The longest segment of 1's is "111" with length 3. There are no 0's, so the longest segment of 0's has length 0. Since 3 > 0, return true.

Visualization

Tap to expand
๐Ÿ Segment Racing: 1's vs 0'sFinding the longest contiguous segments in "110100010"110100010012345678๐Ÿ† Segment Records:๐ŸŸข Winning Streaks (1's)Current streak: 0Record: 2 consecutive wins!๐Ÿ”ด Losing Streaks (0's)Current streak: 1Record: 3 consecutive losses!Segment Breakdown:"11" (len=2)"0" (1)"1" (1)"000" (len=3)"1" (1)"0" (1)๐Ÿ Race ResultLongest 1's segment: 2 charactersLongest 0's segment: 3 charactersWinner: 0's! (3 > 2) โ†’ Return false
Understanding the Visualization
1
Initialize Tracking
Set up counters for current segment length and maximum lengths seen so far
2
Scan Characters
Move through each character, extending current segment or starting a new one
3
Update Records
When a segment ends, check if it beats the current record for that character type
4
Final Comparison
Compare the longest 1's segment with longest 0's segment to get the answer
Key Takeaway
๐ŸŽฏ Key Insight: By tracking segment lengths in a single pass, we can efficiently determine the winner without needing to store or re-examine any part of the string, achieving optimal O(n) time and O(1) space complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, examining each character exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a constant number of variables regardless of input size

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 500
  • s consists only of characters '0' and '1'
Asked in
Amazon 25 Google 15 Microsoft 12 Meta 8
28.5K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen