Skip to content
LC-1796 Easy LeetCode

1796. Second Largest Digit in a String

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 52% Topics: Hash Table, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def secondHighest(self, s):
        """
        :type s: str
        :rtype: int
        """
        first = second = -1
        for c in s:
            if not c.isdigit():
                continue
            d = int(c)
            if d > first:
                first, second = d, first
            elif first > d > second:
                second = d
        return second

Solution from kamyu104/LeetCode-Solutions · MIT