LeetCode 1796. Second Largest Digit in a String
Input: s = "dfa12321afd"
Output: 2
Explanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.Input: s = "abc1111"
Output: -1
Explanation: The digits that appear in s are [1]. There is no second largest digit. Solution
class Solution {
public:
int secondHighest(string s) {
set<char> nums;
for (char ch : s) {
if (isdigit(ch)) {
nums.insert(ch);
}
}
if (nums.size() < 2) {
return -1;
}
return *(++nums.rbegin()) - '0';
}
};PreviousLeetCode 1794. Count Pairs of Equal Substrings With Minimum DifferenceNextLeetCode 1797. Design Authentication Manager
Last updated