Skip to content

Commit 3a928de

Browse files
authored
Create second-largest-digit-in-a-string.py
1 parent e80f470 commit 3a928de

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def secondHighest(self, s):
6+
"""
7+
:type s: str
8+
:rtype: int
9+
"""
10+
first = second = -1
11+
for c in s:
12+
if not c.isdigit():
13+
continue
14+
d = int(c)
15+
if d > first:
16+
first, second = d, first
17+
elif first > d > second:
18+
second = d
19+
return second

0 commit comments

Comments
 (0)