LeetCode solutions

1904. Second Largest Digit in a String

My accepted Python solution to LeetCode problem 1904, Second Largest Digit in a String, running in 4ms.

  • Difficulty: Easy
  • Python
  • Runtime 4ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.4MB, accepted 2025-12-30.

python
class Solution:
    def secondHighest(self, s: str) -> int:
        digits = set()
        for c in s:
            if c.isdigit():
                digits.add(int(c))
        if len(digits) < 2:
            return -1
        sorted_digits = sorted(digits, reverse=True)
        return sorted_digits[1]

Source