Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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.
Example 1:
Input: s = "dfa12321afd" Output: 2 Explanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.
Example 2:
Input: s = "abc1111" Output: -1 Explanation: The digits that appear in s are [1]. There is no second largest digit.
Constraints:
1 <= s.length <= 500s consists of only lowercase English letters and digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"dfa12321afd"
"abc1111"
remove-digit-from-number-to-maximize-result)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1796: Second Largest Digit in a String
class Solution {
public int secondHighest(String s) {
int a = -1, b = -1;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
int v = c - '0';
if (v > a) {
b = a;
a = v;
} else if (v > b && v < a) {
b = v;
}
}
}
return b;
}
}
// Accepted solution for LeetCode #1796: Second Largest Digit in a String
func secondHighest(s string) int {
a, b := -1, -1
for _, c := range s {
if c >= '0' && c <= '9' {
v := int(c - '0')
if v > a {
b, a = a, v
} else if v > b && v < a {
b = v
}
}
}
return b
}
# Accepted solution for LeetCode #1796: Second Largest Digit in a String
class Solution:
def secondHighest(self, s: str) -> int:
a = b = -1
for c in s:
if c.isdigit():
v = int(c)
if v > a:
a, b = v, a
elif b < v < a:
b = v
return b
// Accepted solution for LeetCode #1796: Second Largest Digit in a String
impl Solution {
pub fn second_highest(s: String) -> i32 {
let mut first = -1;
let mut second = -1;
for c in s.as_bytes() {
if char::is_digit(*c as char, 10) {
let num = (c - b'0') as i32;
if first < num {
second = first;
first = num;
} else if num < first && second < num {
second = num;
}
}
}
second
}
}
// Accepted solution for LeetCode #1796: Second Largest Digit in a String
function secondHighest(s: string): number {
let first = -1;
let second = -1;
for (const c of s) {
if (c >= '0' && c <= '9') {
const num = c.charCodeAt(0) - '0'.charCodeAt(0);
if (first < num) {
[first, second] = [num, first];
} else if (first !== num && second < num) {
second = num;
}
}
}
return second;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.