class Solution:
def secondHighest(self, s: str) -> int:
"""tc O(N) sc O(1)
digits : 0 - 9
"""
fst, snd = -1, -1
for c in s:
if c.isdigit():
x = int(c)
if fst < x :
fst,snd = x, fst
elif snd < x and x < fst: # prevent case x == fst
snd = x
return snd
Bit Mask solution
class Solution:
def secondHighest(self, s: str) -> int:
"""tc O(N) sc O(1)
digits : 0 - 9
"""
d = 0
for c in s:
if c.isdigit():
x = int(c)
d |= 1 << x
for i in range(8,-1,-1):
if (d & (1<<i) and d >= (1<<(i+1))) :# there is a number larger than i :
return i
return -1