Given an alphanumeric string s
, return the second largest numerical digit that appears in s
, or -1
if it does not exist.
An alphanumeric string is a string consisting of lowercase English letters and digits.
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.
1 <= s.length <= 500
s
consists of only lowercase English letters and/or digits.
impl Solution {
pub fn second_highest(s: String) -> i32 {
let mut digits = [false; 10];
for c in s.bytes() {
if c.is_ascii_digit() {
digits[(c - b'0') as usize] = true;
}
}
(0..=9)
.rev()
.skip_while(|&x| !digits[x as usize])
.skip(1)
.find(|&x| digits[x as usize])
.unwrap_or(-1)
}
}