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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 4ms, memory 17.4MB, accepted 2025-12-30.
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]