|
| 1 | +--- |
| 2 | +id: second-largest-digit-in-a-string |
| 3 | +title: Second Largest Digit in a String |
| 4 | +sidebar_label: 1796-Second Largest Digit in a String |
| 5 | +tags: |
| 6 | + - Leet code |
| 7 | +description: "Solution to leetocde 1796" |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Statement |
| 11 | + |
| 12 | +Given an alphanumeric string `s`, return the second largest numerical digit that appears in `s`, or -1 if it does not exist. |
| 13 | + |
| 14 | +An alphanumeric string is a string consisting of lowercase English letters and digits. |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +### Example 1 |
| 19 | + |
| 20 | +**Input:** `s = "dfa12321afd"` |
| 21 | +**Output:** `2` |
| 22 | +**Explanation:** The digits that appear in `s` are [1, 2, 3]. The second largest digit is 2. |
| 23 | + |
| 24 | +### Example 2 |
| 25 | + |
| 26 | +**Input:** `s = "abc1111"` |
| 27 | +**Output:** `-1` |
| 28 | +**Explanation:** The digits that appear in `s` are [1]. There is no second largest digit. |
| 29 | + |
| 30 | +## Constraints |
| 31 | + |
| 32 | +- $1 <= s.length <= 500$ |
| 33 | +- s consists of only lowercase English letters and/or digits. |
| 34 | + |
| 35 | +## Algorithm |
| 36 | + |
| 37 | +1. Extract all unique digits from the string. |
| 38 | +2. If the count of unique digits is less than 2, return -1. |
| 39 | +3. Convert the digits to integers and sort them in descending order. |
| 40 | +4. Return the second largest digit. |
| 41 | + |
| 42 | +## Python Code |
| 43 | + |
| 44 | +```python |
| 45 | +class Solution: |
| 46 | + def secondHighest(self, s: str) -> int: |
| 47 | + digits = {char for char in s if char.isdigit()} |
| 48 | + if len(digits) < 2: |
| 49 | + return -1 |
| 50 | + sorted_digits = sorted(map(int, digits), reverse=True) |
| 51 | + return sorted_digits[1] |
| 52 | +``` |
| 53 | + |
| 54 | +## C++ Code |
| 55 | + |
| 56 | +```cpp |
| 57 | +#include <string> |
| 58 | +#include <set> |
| 59 | +#include <algorithm> |
| 60 | + |
| 61 | +class Solution { |
| 62 | +public: |
| 63 | + int secondHighest(std::string s) { |
| 64 | + std::set<char> digits; |
| 65 | + for (char ch : s) { |
| 66 | + if (isdigit(ch)) { |
| 67 | + digits.insert(ch); |
| 68 | + } |
| 69 | + } |
| 70 | + if (digits.size() < 2) { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + auto it = digits.rbegin(); |
| 74 | + std::advance(it, 1); |
| 75 | + return *it - '0'; |
| 76 | + } |
| 77 | +}; |
| 78 | +``` |
| 79 | +
|
| 80 | +## Java Code |
| 81 | +
|
| 82 | +```java |
| 83 | +import java.util.Set; |
| 84 | +import java.util.TreeSet; |
| 85 | +
|
| 86 | +class Solution { |
| 87 | + public int secondHighest(String s) { |
| 88 | + Set<Character> digits = new TreeSet<>(); |
| 89 | + for (char ch : s.toCharArray()) { |
| 90 | + if (Character.isDigit(ch)) { |
| 91 | + digits.add(ch); |
| 92 | + } |
| 93 | + } |
| 94 | + if (digits.size() < 2) { |
| 95 | + return -1; |
| 96 | + } |
| 97 | + return Character.getNumericValue(((TreeSet<Character>) digits).lower(((TreeSet<Character>) digits).last())); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## JavaScript Code |
| 103 | + |
| 104 | +```javascript |
| 105 | +/** |
| 106 | + * @param {string} s |
| 107 | + * @return {number} |
| 108 | + */ |
| 109 | +var secondHighest = function (s) { |
| 110 | + let digits = new Set(); |
| 111 | + for (let char of s) { |
| 112 | + if (/\d/.test(char)) { |
| 113 | + digits.add(char); |
| 114 | + } |
| 115 | + } |
| 116 | + if (digits.size < 2) { |
| 117 | + return -1; |
| 118 | + } |
| 119 | + let sortedDigits = Array.from(digits) |
| 120 | + .map(Number) |
| 121 | + .sort((a, b) => b - a); |
| 122 | + return sortedDigits[1]; |
| 123 | +}; |
| 124 | +``` |
0 commit comments