↓Skip to main content
  1. LeetCode/

LeetCode 1869: Longer Contiguous Segments of Ones than Zeros

·1 min· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-24

  • Submissions: Wrong Answer ×2 → Accepted ×2

Approach
#

Splitting on '0' leaves runs of 1s, so the longest piece is the longest run of 1s; splitting on '1' likewise gives 0s. The 1s must be strictly longer.

My Attempts
#

The first two counted runs by hand but tested i == 1, and a string character never equals the integer 1, so every character counted as a 0: "1" returned False. Attempt 1 also skipped updating the final run after the loop. A one-line split version passed.

Solution
#

class Solution:
    def checkZeroOnes(self, s: str) -> bool:
        return max(map(len, s.split("0"))) > max(map(len, s.split("1")))

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • The characters are '1', not the integer 1.
  • When counting runs by hand, update once more after the loop for the final run.

Interview Explanation
#

Splitting on 0 gives runs of 1s and splitting on 1 gives runs of 0s; I compare their longest pieces. Time O(n).

Related

LeetCode 1446: Consecutive Characters
·1 min
LeetCode Easy String
LeetCode 1446 note: track the current run of equal characters in one pass, resetting to 1 on a change
LeetCode 415: Add Strings
·2 mins
LeetCode Easy Math String Simulation
LeetCode 415 note: add digit by digit from the ends with two pointers, tracking the carry, and append any leftover carry after the loop
LeetCode 459: Repeated Substring Pattern
·1 min
LeetCode Easy String String-Matching Z-Algorithm Knuth-Morris-Pratt-Algorithm
LeetCode 459 note: s is a repetition of a substring exactly when s occurs in s + s with its first and last characters removed
LeetCode 482: License Key Formatting
·2 mins
LeetCode Easy String
LeetCode 482 note: remove every dash and uppercase first, then cut groups of k from the end so only the first group may be shorter
LeetCode 290: Word Pattern
·1 min
LeetCode Easy Hash-Table String
LeetCode 290 note: pattern characters and words must correspond one to one; use two dicts or compare set sizes
LeetCode 344: Reverse String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 344 note: swap with two pointers and move inward to reverse in place