Skip to content

Commit ec1e02e

Browse files
committed
Create 1796-SecondLargestDigitInAString.py
1 parent ce4f714 commit ec1e02e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
"""
3+
CREATED AT: 2022-12-03
4+
5+
URL: https://leetcode.com/problems/second-largest-digit-in-a-string/
6+
7+
GITHUB: https://github.com/Jiezhi/myleetcode
8+
9+
FileName: 1796-SecondLargestDigitInAString
10+
11+
Difficulty: Easy
12+
13+
Desc:
14+
15+
Tag:
16+
17+
See:
18+
19+
"""
20+
21+
22+
class Solution:
23+
def secondHighest(self, s: str) -> int:
24+
"""
25+
Runtime: 41 ms, faster than 89.78%
26+
Memory Usage: 13.8 MB, less than 97.45%
27+
1 <= s.length <= 500
28+
s consists of only lowercase English letters and/or digits.
29+
"""
30+
m1, m2 = -1, -1
31+
for c in s:
32+
if c.isdigit():
33+
n = int(c)
34+
if n > m1:
35+
m1, m2 = n, m1
36+
elif n != m1 and n > m2:
37+
m2 = n
38+
return m2
39+
40+
41+
def test():
42+
assert Solution().secondHighest(s="dfa12321afd") == 2
43+
assert Solution().secondHighest(s="abc1111") == -1
44+
45+
46+
if __name__ == '__main__':
47+
test()

0 commit comments

Comments
 (0)