1869. Longer Contiguous Segments of Ones than Zeros LeetCode Solution

In this guide, you will get 1869. Longer Contiguous Segments of Ones than Zeros LeetCode Solution with the best time and space complexity. The solution to Longer Contiguous Segments of Ones than Zeros problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longer Contiguous Segments of Ones than Zeros solution in C++
  4. Longer Contiguous Segments of Ones than Zeros solution in Java
  5. Longer Contiguous Segments of Ones than Zeros solution in Python
  6. Additional Resources
1869. Longer Contiguous Segments of Ones than Zeros LeetCode Solution image

Problem Statement of Longer Contiguous Segments of Ones than Zeros

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.

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'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1869. Longer Contiguous Segments of Ones than Zeros LeetCode Solution in C++

class Solution {
 public:
  bool checkZeroOnes(string s) {
    int longestOnes = 0;
    int longestZeros = 0;
    int currentOnes = 0;
    int currentZeros = 0;

    for (const char c : s)
      if (c == '0') {
        currentOnes = 0;
        longestZeros = max(longestZeros, ++currentZeros);
      } else {
        currentZeros = 0;
        longestOnes = max(longestOnes, ++currentOnes);
      }

    return longestOnes > longestZeros;
  }
};
/* code provided by PROGIEZ */

1869. Longer Contiguous Segments of Ones than Zeros LeetCode Solution in Java

class Solution {
  public boolean checkZeroOnes(String s) {
    int longestOnes = 0;
    int longestZeros = 0;
    int currentOnes = 0;
    int currentZeros = 0;

    for (final char c : s.toCharArray())
      if (c == '0') {
        currentOnes = 0;
        longestZeros = Math.max(longestZeros, ++currentZeros);
      } else {
        currentZeros = 0;
        longestOnes = Math.max(longestOnes, ++currentOnes);
      }

    return longestOnes > longestZeros;
  }
}
// code provided by PROGIEZ

1869. Longer Contiguous Segments of Ones than Zeros LeetCode Solution in Python

class Solution:
  def checkZeroOnes(self, s: str) -> bool:
    longestOnes = 0
    longestZeros = 0
    currentOnes = 0
    currentZeros = 0

    for c in s:
      if c == '0':
        currentOnes = 0
        currentZeros += 1
        longestZeros = max(longestZeros, currentZeros)
      else:
        currentZeros = 0
        currentOnes += 1
        longestOnes = max(longestOnes, currentOnes)

    return longestOnes > longestZeros
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.