Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 537 Bytes

1796.md

File metadata and controls

22 lines (20 loc) · 537 Bytes

1796. Second Largest Digit in a String

Solution 1 (time O(n), space O(1))

class Solution(object):
    def secondHighest(self, s):
        """
        :type s: str
        :rtype: int
        """
        max1, max2 = -1, -1
        for c in s:
            if ord('0') <= ord(c) <= ord('9'):
                t = int(c)
                if t > max1:
                    max2 = max1
                    max1 = t
                elif max1 > t > max2:
                    max2 = t
        return max2