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

Note: This problem is from LeetCode.
Compiled
Executed
⚠️ Correct
program main
    ! Solves the problem described in the comment block
    implicit none

    character(len=100) :: s
    logical :: result

    ! Example 1
    s = "1101 "
    result = solve(s)
    write (unit=*, fmt="(L1)") result

    ! Example 2
    s = "111000 "
    result = solve(s)
    write (unit=*, fmt="(L1)") result

    ! Example 3
    s = "110100010 "
    result = solve(s)
    write (unit=*, fmt="(L1)") result

contains

    function solve(s) result(result)
        ! Returns true if the longest contiguous segment of 1s is strictly
        ! longer than the longest contiguous segment of 0s in s, or returns
        ! false otherwise
        implicit none
        character(len=*), intent(in) :: s
        integer :: i, j, n, m
        logical :: result

        n = len(s)
        m = 0
        do i = 1, n
            if (s(i:i) == '1') then
                j = i
                do while (j <= n .and. s(j:j) == '1')
                    j = j + 1
                end do
                if (j - i > m) then
                    m = j - i
                end if
            else if (s(i:i) == '0') then
                j = i
                do while (j <= n .and. s(j:j) == '0')
                    j = j + 1
                end do
                if (j - i > m) then
                    m = j - i
                end if
            end if
        end do

        result = m > 0 .and. m > n - m
    end function solve
end program main
Compiled
Executed
Correct
!-------------------------------------------------------------------------------
! Copyright (c) 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030,
! 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043,
! 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056,
! 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069,
! 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082,
! 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094, 2095,
! 2096, 2097, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105, 2106, 2107, 2108,
! 2109, 2110, 2111, 2112, 2113, 2114, 2115, 2116, 2117, 2118, 2119, 2120, 2121,
! 2122, 2123, 2124, 2125, 2126, 2127, 2128, 2129, 2130, 2131, 2132, 2133, 2134,
! 2135, 2136, 2137, 2138, 2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147,
! 2148, 2149, 2150, 2151, 2152, 2153, 2154, 2155, 2156, 2157, 2158, 2159, 2160,
! 2161, 2162, 2163, 2164, 2165, 2166, 2167, 2168, 2169, 2170, 2171, 2172, 2173,
! 2174, 2175, 2176, 2177, 2178, 2179, 2180, 2181, 2182, 2183,
🌐 Data from online sources
def checkZeroOnes(s: str) -> bool:
    max_ones, max_zeros, current_ones, current_zeros = 0, 0, 0, 0
    for c in s:
        if c == '1':
            current_ones += 1
            current_zeros = 0
        else:
            current_zeros += 1
            current_ones = 0
        max_ones = max(max_ones, current_ones)
        max_zeros = max(max_zeros, current_zeros)
    return max_ones > max_zeros

We initialize 4 variables: max_ones, max_zeros, current_ones, and current_zeros. We then iterate through each character in the string. If we find 1, we increment the current_ones counter and set the current_zeros count to 0, and if we find 0, we increment the current_zeros counter and set the current_ones counter to 0. After each character, we update the maximum values of max_ones and max_zeros using the max function or Math.max function depending on the language. Finally, after iterating through the entire string, we compare the max_ones and max_zeros to determine if the longest continuous segment of 1s is strictly longer than that of 0s and return the result.

🌐 Data from online sources
bool checkZeroOnes(string s) {
    int max_ones = 0, max_zeros = 0, current_ones = 0, current_zeros = 0;
    for (char c : s) {
        if (c == '1') {
            current_ones++;
            current_zeros = 0;
        } else {
            current_zeros++;
            current_ones = 0;
        }
        max_ones = max(max_ones, current_ones);
        max_zeros = max(max_zeros, current_zeros);
    }
    return max_ones > max_zeros;
}

We initialize 4 variables: max_ones, max_zeros, current_ones, and current_zeros. We then iterate through each character in the string. If we find 1, we increment the current_ones counter and set the current_zeros count to 0, and if we find 0, we increment the current_zeros counter and set the current_ones counter to 0. After each character, we update the maximum values of max_ones and max_zeros using the max function or Math.max function depending on the language. Finally, after iterating through the entire string, we compare the max_ones and max_zeros to determine if the longest continuous segment of 1s is strictly longer than that of 0s and return the result.