Date: 2026-06-06
Time: 19:00
second-largest-digit-in-a-string/solution.pyThis file solves LeetCode 1796 — Second Largest Digit in a String. It owns a single responsibility: given an alphanumeric string, extract all unique digit characters and return the second-largest, or -1 if fewer than two distinct digits exist.
second_highest(s: str) -> int — The sole public function.
s is a string of lowercase English letters and/or digits (per the LeetCode constraint).int, or -1 if the string contains fewer than two distinct digits.Set-based deduplication: Rather than sorting or maintaining a top-two tracker, the solution collects all digits into a set, which naturally deduplicates. This is a common idiom in this repo's easy-tier solutions — favor clarity and Python builtins over manual state management.
Two-pass max extraction: Instead of sorting the set (O(n log n)) or using heapq.nlargest, it calls max() twice with a remove() in between. This is O(n) in the size of the digit set, which is bounded at 10 elements — so performance is constant regardless of input length. The string scan itself is O(len(s)).
Imports: None — pure stdlib, no external or internal imports.
Imported by: The "Imported By" list in the prompt is misleading — those are test files across hundreds of other problems. They don't actually import this module. The only real consumer is second-largest-digit-in-a-string/test_solution.py, which tests this function directly.
1. Filter & convert: {int(c) for c in s if c.isdigit()} — single pass over the string, extracting digit characters and converting to int. The set comprehension deduplicates.
2. Early return: If the resulting set has fewer than 2 elements, return -1.
3. Remove max: digits.remove(max(digits)) mutates the set in-place, dropping the largest digit.
4. Return new max: max(digits) on the reduced set yields the second-largest.
digits.remove(max(digits)) is safe because the len(digits) < 2 guard ensures at least 2 elements remain when reached.max() would fail on an empty set.No exceptions are raised or caught. The -1 sentinel value serves as the error/absence signal, matching the LeetCode problem specification. The len(digits) < 2 check prevents max() from being called on an empty set after removal.
second-largest-digit-in-a-string/test_solution.py — See what edge cases are covered (all-letters, single digit, all same digit, mixed)second-largest-digit-in-a-string/review.md — Code review notes may flag alternative approaches or known issuesthird-maximum-number/solution.py:thirdMax — Similar "k-th largest" problem with a more complex approach (3rd max with distinct-value semantics)number-of-different-integers-in-a-string/solution.py — Related string-digit-extraction problem, likely uses a different parsing strategyset-based-digit-extraction — Pattern recurs across digit-manipulation problems in this repo; worth comparing approachessecond-highest-returns-minus-one-on-fewer-than-two-distinct-digits — second_highest returns -1 when the input contains zero or one distinct digit characterssecond-highest-is-o-n-in-string-length — The function's time complexity is O(len(s)) since the digit set is bounded at size 10second-highest-mutates-local-set-not-input — The set is constructed internally; the input string s is never modifiedsecond-highest-no-imports — The module has zero imports, relying entirely on Python builtins (set, max, int, str.isdigit)