Date: 2026-06-06
Time: 16:57
greatest-english-letter-in-upper-and-lower-case/solution.pyThis file solves LeetCode 2309: given a string of English letters, find the greatest (alphabetically last) letter that appears in both uppercase and lowercase forms. Return the uppercase version, or "" if no such letter exists.
getmaxoccurrences(s: str) -> str — The sole function. Despite its name (which suggests frequency counting), it actually finds the greatest letter present in both cases. The contract: accepts any string of English letters, returns a single uppercase character or the empty string.
Reverse-alphabet scan. Rather than collecting all dual-case letters and taking the max, the function iterates "ZYXWVUTSRQPONMLKJIHGFEDCBA" — the alphabet in descending order. The first match is guaranteed to be the greatest, so it returns immediately. This is a classic early-exit optimization: worst case is 26 iterations (no match), best case is 1 (letter Z qualifies).
Set-based membership. The input string is converted to a set on line 12 before any lookups. This turns each in check from O(n) to O(1), making the overall function O(n) where n is the length of s, rather than O(26n).
Imports: None — pure standard Python, no library dependencies.
Imported by: The "Imported By" list is misleading. Those 400+ test files don't actually import *this* solution — they import their own solution.py via a shared test harness pattern. The only genuine consumer is greatest-english-letter-in-upper-and-lower-case/test_solution.py.
1. Convert s to a set of characters (O(n)).
2. Walk the uppercase alphabet from Z down to A.
3. For each uppercase letter c, check if both c and c.lower() exist in the set.
4. Return the first c that passes (greatest dual-case letter).
5. If the loop exhausts without a match, return "".
max() call.s, no side effects.None. The function assumes valid input (a string of English letters per the LeetCode constraint). An empty string input naturally produces "" — the set will be empty, no uppercase letter matches, and the fallback return fires. Non-letter characters in s are harmless; they'll land in the set but never match an uppercase letter check.
The function name getmaxoccurrences is a misnomer — it doesn't count occurrences. A clearer name would be greatestLetter or greatestdualcase_letter. This is likely an artifact of the automated generation pipeline used across this repo.
greatest-english-letter-in-upper-and-lower-case/test_solution.py — See what edge cases the test suite covers (empty string, single case only, all 26 letters present)greatest-english-letter-in-upper-and-lower-case/review.md — Check if the code review flagged the misleading function namelongest-nice-substring/solution.py:getmaxoccurrences — Another dual-case letter problem that likely uses a similar set-based pattern; compare approachesfunction-naming-conventions — Whether the getmaxoccurrences name is a repo-wide template artifact or specific to this solutionrun_tests.py — Understand the shared test harness that makes these solutions appear cross-importedreverse-alpha-scan-gives-max — Iterating "ZYXWVUTSRQPONMLKJIHGFEDCBA" and returning on first match guarantees the lexicographically greatest result without needing max()set-conversion-is-o1-lookup — Converting s to a set before the loop ensures each membership check is O(1), making the function O(n + 26) overallfunction-name-mismatches-behavior — getmaxoccurrences does not count occurrences; it finds the greatest letter appearing in both casesempty-input-returns-empty-string — When s is empty, the function returns "" via the fallback path with no special-case handling needed