Leetcode Problem 1869. Longer Contiguous Segments of Ones than Zeros

1869. Longer Contiguous Segments of Ones than Zeros

Leetcode Solutions

Counting Contiguous Segments

  1. Initialize four variables: max1 and max0 to store the maximum lengths of contiguous 1s and 0s, and count1 and count0 to count the current lengths of contiguous 1s and 0s.
  2. Iterate through each character in the string s.
    • If the current character is '1', increment count1 and reset count0 to zero.
    • If the current character is '0', increment count0 and reset count1 to zero.
    • Update max1 with the maximum of max1 and count1.
    • Update max0 with the maximum of max0 and count0.
  3. After the loop, compare max1 and max0.
    • If max1 is greater than max0, return true.
    • Otherwise, return false.
UML Thumbnail

Two-Pointer Technique

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...