Second Largest Digit in a String — Problem & Solution
Second Largest Digit in a String solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Second Largest Digit in a String is an easy coding problem on strings and hash table, asked in Amazon and Google interviews.
- Difficulty: Easy
- Topics: Strings, Hash Table
- Asked at: Amazon, Google
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
The string s mixes lowercase letters and digits.
Among the distinct digits that appear in s, return the second largest one. If fewer than two distinct digits appear, return -1.
Example 1
Input: s = "kairo2026x9"
Output: 6
Explanation: The distinct digits are 0, 2, 6 and 9.
Example 2
Input: s = "codekairo7"
Output: -1
Explanation: Only one distinct digit.
Example 3
Input: s = "a5b3c5"
Output: 3
Constraints
1 <= s.length <= 500s consists of only lowercase English letters and digits
Hints for Second Largest Digit in a String
Hint 1
Letters can be ignored entirely.
Hint 2
Repeated digits count once — you need the two largest different digits.
Hint 3
Keep
firstandsecond(both starting at -1) and update them as you see each digit; a digit equal tofirstchanges nothing.
How to solve Second Largest Digit in a String
Track the largest and second-largest distinct digits in one pass, the same way one finds the top two values of an array, skipping duplicates of the current maximum.
Approach
- Set
first = second = -1. - For each digit character with value
d: ifd > first, shiftsecond = firstand setfirst = d. - Otherwise, if
second < d < first, setsecond = d. - Return
second.
Why it works
After each step first is the largest digit seen and second the largest digit seen that is strictly smaller than first (or -1). A new maximum demotes the old one to second place; a value strictly between them improves second; anything else changes neither. Using strict comparisons makes duplicates harmless.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
"99"has only one distinct digit — the answer is -1, not 9.- When a new maximum arrives, the old maximum must move to
second. 0is a real digit; initialise with -1, not 0.
Reference solution
Python
def secondHighest(s: str) -> int:
first = second = -1
for c in s:
if c.isdigit():
d = int(c)
if d > first:
first, second = d, first
elif second < d < first:
second = d
return secondJava
public static int secondHighest(String s) {
int first = -1, second = -1;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') continue;
int d = c - '0';
if (d > first) {
second = first;
first = d;
} else if (d < first && d > second) {
second = d;
}
}
return second;
}C++
int secondHighest(string s) {
int first = -1, second = -1;
for (char c : s) {
if (c < '0' || c > '9') continue;
int d = c - '0';
if (d > first) {
second = first;
first = d;
} else if (d < first && d > second) {
second = d;
}
}
return second;
}JavaScript
var secondHighest = function(s) {
var first = -1, second = -1;
for (var i = 0; i < s.length; i++) {
var c = s.charCodeAt(i);
if (c < 48 || c > 57) continue;
var d = c - 48;
if (d > first) {
second = first;
first = d;
} else if (d < first && d > second) {
second = d;
}
}
return second;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Hashing: Hash Maps and Hash Sets