1796. Second Largest Digit in a String
Solutions
class Solution {
public:
int secondHighest(string s) {
vector<int> count(10);
for (auto c : s) {
if (c >= '0' && c <= '9') {
count[c - '0']++;
}
}
for (int i = 9; i >= 0; i--) {
if (count[i] > 0) {
for (int j = i - 1; j >= 0; j--) {
if (count[j] > 0)
return j;
}
break;
}
}
return -1;
}
};Last updated