Second Largest Digit in a String

🏠 ⬅️ ➡️

Given an alphanumeric string s, return the second largest numerical digit that appears in s, or -1 if it does not exist.

An alphanumeric string is a string consisting of lowercase English letters and digits.

Example 1:

Input: s = "dfa12321afd " Output: 2 Explanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.

Example 2:

Input: s = "abc1111 " Output: -1 Explanation: The digits that appear in s are [1]. There is no second largest digit.

Constraints:

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters and/or digits.

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

    character(len=500) :: s
    integer :: i, j, n, m, result

    ! Example 1
    s = "dfa12321afd "
    write (*,*) 'Example 1:'
    write (*,*) 'Input: ', s
    result = second_largest_digit(s)
    write (*,*) 'Output: ', result
    write (*,*) ''

    ! Example 2
    s = "abc1111 "
    write (*,*) 'Example 2:'
    write (*,*) 'Input: ', s
    result = second_largest_digit(s)
    write (*,*) 'Output: ', result
    write (*,*) ''

contains

    function second_largest_digit(s) result(result)
        implicit none
        character(len=*), intent(in) :: s
        integer :: result
        logical :: found

        found = .false.
        result = -1
        do i = 1, len(s)
            if (is_digit(s(i:i))) then
                if (.not. found) then
                    found = .true.
                    result = ichar(s(i:i)) - ichar('0')
                else
                    if (ichar(s(i:i)) - ichar('0') > result) then
                        result = ichar(s(i:i)) - ichar('0')
                    end if
                end if
            end if
        end do
    end function second_largest_digit

    function is_digit(c) result(res)
        implicit none
        character(len=*), intent(in) :: c
        logical :: res

        res = any(c == ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
    end function is_digit

end program main
Compiled
Executed
Correct
program largest_digit
    implicit none

    character(len=*) :: s
    integer :: i, j, largest, second_largest, digit

    largest = -1
    second_largest = -1

    do i = 1, len_trim(s)
        read(s(i:i), '(i1)') digit
        if (digit >= 0 .and. digit <= 9) then
            if (digit > largest) then
                second_largest = largest
                largest = digit
            else if (digit > second_largest) then
                second_largest = digit
            end if
        end if
    end do

    if (second_largest == -1) then
        write (*, '(i1)') -1
    else
        write (*, '(i1)') second_largest
    end if

end program largest_digit
🌐 Data from online sources
def second_highest(s: str) -> int:
    largest = -1
    second_largest = -1
    for c in s:
        if c.isdigit():
            d = int(c)
            if d > largest:
                second_largest = largest
                largest = d
            elif d < largest and d > second_largest:
                second_largest = d
    return second_largest

To solve this problem, we keep track of the largest and second largest numerical digit, initializing them to -1. We iterate through each character in the input string, check if it's a digit using isdigit function in C++, Character.isDigit in Java, c.isdigit() in Python, and !isNaN(c) in JavaScript. If it's a digit, we convert it to an integer and then compare it with the current largest and second largest values. 1. If the digit is greater than largest, we update second_largest to the largest value, and set largest to the digit. 2. If the digit is not equal to the largest value and greater than second_largest, we update second_largest to the digit. After the loop, we return the second_largest value. If no second largest digit exists, the value will still be -1.

🌐 Data from online sources
int secondHighest(string s) {
    int largest = -1, second_largest = -1;
    for (char c : s) {
        if (isdigit(c)) {
            int d = c - '0';
            if (d > largest) {
                second_largest = largest;
                largest = d;
            } else if (d < largest && d > second_largest) {
                second_largest = d;
            }
        }
    }
    return second_largest;
}

To solve this problem, we keep track of the largest and second largest numerical digit, initializing them to -1. We iterate through each character in the input string, check if it's a digit using isdigit function in C++, Character.isDigit in Java, c.isdigit() in Python, and !isNaN(c) in JavaScript. If it's a digit, we convert it to an integer and then compare it with the current largest and second largest values. 1. If the digit is greater than largest, we update second_largest to the largest value, and set largest to the digit. 2. If the digit is not equal to the largest value and greater than second_largest, we update second_largest to the digit. After the loop, we return the second_largest value. If no second largest digit exists, the value will still be -1.