Greatest English Letter in Upper and Lower Case

🏠 ⬅️ ➑️

Given a string of English letters s, return the greatest English letter which occurs as both a lowercase and uppercase letter in s. The returned letter should be in uppercase. If no such letter exists, return an empty string.

An English letter b is greater than another letter a if b appears after a in the English alphabet.

Example 1:

Input: s = "lEeTcOdE " Output: "E " Explanation: The letter 'E' is the only letter to appear in both lower and upper case.

Example 2:

Input: s = "arRAzFif " Output: "R " Explanation: The letter 'R' is the greatest letter to appear in both lower and upper case. Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.

Example 3:

Input: s = "AbCdEfGhIjK " Output: " " Explanation: There is no letter that appears in both lower and upper case.

Constraints:

  • 1 <= s.length <= 1000
  • s consists of lowercase and uppercase English letters.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=:), allocatable :: s
    character(len=1) :: result

    s = "lEeTcOdE "
    write (*,*) 'Example 1:'
    write (*,'(A)') 'Input: ', trim(s)
    call solve(s, result)
    write (*,'(A,A)') 'Output: ', trim(result)
    ! Output: Example 1:
    ! Input: lEeTcOdE
    ! Output: E

    s = "aRRazFif "
    write (*,*) 'Example 2:'
    write (*,'(A)') 'Input: ', trim(s)
    call solve(s, result)
    write (*,'(A,A)') 'Output: ', trim(result)
    ! Output: Example 2:
    ! Input: aRRazFif
    ! Output: R

    s = "AbCdEfGhIjK "
    write (*,*) 'Example 3:'
    write (*,'(A)') 'Input: ', trim(s)
    call solve(s, result)
    write (*,'(A,A)') 'Output: ', trim(result)
    ! Output: Example 3:
    ! Input: AbCdEfGhIjK
    ! Output:

contains

    subroutine solve(s, result)
        implicit none
        character(len=*), intent(in) :: s
        character(len=1), intent(out) :: result

        integer :: i, j, n
        logical :: found

        n = len(s)
        do i = 1, n - 1
            do j = i + 1, n
                if (s(i:i) == s(j:j)) then
                    found = .true.
                    exit
                end if
            end do
            if (found) then
                result = s(i:i)
                exit
            end if
        end do

        if (result == '') then
            result = ''
        else
            result = toupper(result)
        end if

    end subroutine solve

end program main
βœ… Compiled
❌ Executed
❌ Correct
PROGRAM greatest_common_letter

IMPLICIT NONE

CHARACTER(len=1000) :: s
CHARACTER(len=1) :: c
INTEGER :: i, j
LOGICAL :: found

! Read input
READ(*,*) s

! Initialize variables
found = .FALSE.
c = ' '

! Loop through the string
DO i = 1, LEN(s)
    ! Check if the current character is a letter
    IF (s(i:i) >= 'a' .AND. s(i:i) <= 'z') THEN
        ! Check if the current character is also a uppercase letter
        IF (s(i:i) >= 'A' .AND. s(i:i) <= 'Z') THEN
            ! Check if the current character is greater than the previous character
            IF (s(i:i) > c) THEN
                ! Update the greatest common letter
                c = s(i:i)
                found = .TRUE.
            END IF
        END IF
    END IF
END DO

! Print output
IF (found) THEN
    WRITE(*,*) c
ELSE
    WRITE(*,*) ""
END IF

END PROGRAM greatest_common_letter
🌐 Data from online sources
def get_max_occurrences(text: str, pattern: str) -> int:
    count = [0, 0]
    for c in text:
        if c == pattern[1]:
            count[1] += count[0]
        elif c == pattern[0]:
            count[0] += 1
    return count[1]

The algorithm iterates through the text string character by character. Two counters, count[0] and count[1], are used to keep track of the occurrences of pattern[0] and pattern[1], respectively. For each character encountered in the text, we check whether it is equal to pattern[0] or pattern[1]. If the character matches pattern[0], we increment counter count[0]. If the character matches pattern[1], we increment the counter count[1] by count[0], because we want to count the number of subsequences formed with the previously found matches of pattern[0]. Finally, after the iteration, the count of pattern[1] returns the maximum number of occurrences of the given pattern.

🌐 Data from online sources
int getMaxOccurrences(const std::string& text, const std::string& pattern) {
    int count[2] = {0, 0};
    for (char c : text) {
        if (c == pattern[1]) {
            count[1] += count[0];
        }
        else if (c == pattern[0]) {
            count[0] += 1;
        }
    }
    return count[1];
}

The algorithm iterates through the text string character by character. Two counters, count[0] and count[1], are used to keep track of the occurrences of pattern[0] and pattern[1], respectively. For each character encountered in the text, we check whether it is equal to pattern[0] or pattern[1]. If the character matches pattern[0], we increment counter count[0]. If the character matches pattern[1], we increment the counter count[1] by count[0], because we want to count the number of subsequences formed with the previously found matches of pattern[0]. Finally, after the iteration, the count of pattern[1] returns the maximum number of occurrences of the given pattern.