Skip to content

Commit f62f5ec

Browse files
authored
Merge pull request #112 from WazedKhan/1796-second-largest-digit-in-string
1796: Second Largest Digit in a String
2 parents 04d9d4b + 62a86b7 commit f62f5ec

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def secondHighest(self, s: str) -> int:
3+
first = second = -1
4+
for char in s:
5+
if char.isdigit():
6+
d = int(char)
7+
if d > first:
8+
second = first
9+
first = d
10+
if first > d > second:
11+
second = d
12+
return second

tests/test_leetcode_easy.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,26 @@
55
"num1,n,num2,m,expected",
66
[
77
# Test case 1: Normal merge
8-
([1,2,3,0,0,0], 3, [2,5,6], 3, [1,2,2,3,5,6]),
9-
8+
([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3, [1, 2, 2, 3, 5, 6]),
109
# Test case 2: num1 is empty except for zeroes
1110
([0], 0, [1], 1, [1]),
12-
1311
# Test case 3: num2 is empty, num1 should remain the same
1412
([1], 1, [], 0, [1]),
15-
1613
# Test case 4: All elements in num2 are smaller
17-
([4,5,6,0,0,0], 3, [1,2,3], 3, [1,2,3,4,5,6]),
18-
14+
([4, 5, 6, 0, 0, 0], 3, [1, 2, 3], 3, [1, 2, 3, 4, 5, 6]),
1915
# Test case 5: All elements in num2 are larger
20-
([1,2,3,0,0,0], 3, [4,5,6], 3, [1,2,3,4,5,6]),
21-
16+
([1, 2, 3, 0, 0, 0], 3, [4, 5, 6], 3, [1, 2, 3, 4, 5, 6]),
2217
# Test case 6: num1 and num2 have same values
23-
([2,2,3,0,0,0], 3, [2,2,3], 3, [2,2,2,2,3,3]),
24-
18+
([2, 2, 3, 0, 0, 0], 3, [2, 2, 3], 3, [2, 2, 2, 2, 3, 3]),
2519
# Test case 7: One list contains all duplicates
26-
([1,1,1,0,0,0], 3, [1,1,1], 3, [1,1,1,1,1,1]),
27-
20+
([1, 1, 1, 0, 0, 0], 3, [1, 1, 1], 3, [1, 1, 1, 1, 1, 1]),
2821
# Test case 8: Large input where num2 fills all of num1
29-
([0,0,0], 0, [1,2,3], 3, [1,2,3]),
30-
22+
([0, 0, 0], 0, [1, 2, 3], 3, [1, 2, 3]),
3123
# Test case 9: Already merged
32-
([1,2,3], 3, [], 0, [1,2,3]),
33-
24+
([1, 2, 3], 3, [], 0, [1, 2, 3]),
3425
# Test case 10: Mix of negative numbers
35-
([-1,0,0,3,3,3,0,0,0], 6, [1,2,2], 3, [-1,0,0,1,2,2,3,3,3]),
36-
]
26+
([-1, 0, 0, 3, 3, 3, 0, 0, 0], 6, [1, 2, 2], 3, [-1, 0, 0, 1, 2, 2, 3, 3, 3]),
27+
],
3728
)
3829
def test_merge_sorted_array_88(num1, n, num2, m, expected):
3930
"""
@@ -98,3 +89,28 @@ def test_max_profit(prices, expected):
9889

9990
solution = Solution()
10091
assert solution.maxProfit(prices) == expected
92+
93+
94+
@pytest.mark.parametrize(
95+
"s, expected",
96+
[
97+
("dfa12321afd", 2), # normal case → digits: {1,2,3}
98+
("abc1111", -1), # only one unique digit
99+
("abc", -1), # no digits
100+
("ck077", 0), # digits: {0,7} → second highest = 0
101+
("sjhtz8344", 4), # digits: {3,4,8} → second = 4
102+
("aaaa9998", 8), # digits: {8,9} → second = 8
103+
("0", -1), # only one digit
104+
("9876543210", 8), # all digits → second = 8
105+
("2abc3d4e5f6", 5), # scattered digits → second = 5
106+
("99", -1), # duplicates only
107+
("a1b2c3d4e5", 4), # increasing digits
108+
("5a5b5c5d5", -1), # one unique digit seen multiple times
109+
("z1y0x9w", 1), # digits: {0,1,9} → second = 1
110+
],
111+
)
112+
def test_second_highest(s, expected):
113+
from LeetCode.easy.second_largest_digit_in_string_1796 import Solution
114+
115+
solution = Solution()
116+
assert solution.secondHighest(s) == expected

0 commit comments

Comments
 (0)