Skip to content

Commit e80f470

Browse files
authored
Create second-largest-digit-in-a-string.cpp
1 parent 473b566 commit e80f470

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int secondHighest(string s) {
7+
int first = -1, second = -1;
8+
for (const auto& c : s) {
9+
if (!isdigit(c)) {
10+
continue;
11+
}
12+
int d = c - '0';
13+
if (d > first) {
14+
second = first;
15+
first = d;
16+
} else if (first > d && d > second) {
17+
second = d;
18+
}
19+
}
20+
return second;
21+
}
22+
};

0 commit comments

Comments
 (0)