Skip to content

Latest commit

 

History

History
44 lines (35 loc) · 837 Bytes

1796. Second Largest Digit in a String.md

File metadata and controls

44 lines (35 loc) · 837 Bytes

Approach 01

long solution

class Solution:
    def secondHighest(self, s: str) -> int:
        
        def isNumeral(num):
            l=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
            if num in l:
                return True
            else:
                return False
        
        n=[]
        for i in s:
            if isNumeral(i) and i not in n:
                n.append(i)

        if len(n)>=2:
            n.sort()
            return int(n[-2])
        else:
            return -1

Approach 02

class Solution:
    def secondHighest(self, s: str) -> int:
        
        n=[]
        for i in s:
            if i not in n and i in "0123456789":
                n.append(i)

        if len(n)>=2:
            n.sort()
            return int(n[-2])
        else:
            return -1