Greatest English Letter in Upper and Lower Case - Problem

Given a string of English letters s, return the greatest English letter which occurs as both a lowercase and uppercase letter in s. The returned letter should be in uppercase. If no such letter exists, return an empty string.

An English letter b is greater than another letter a if b appears after a in the English alphabet.

Input & Output

Example 1 — Basic Case
$ Input: s = "lEeTcOdE"
Output: "E"
💡 Note: The string contains both 'E' and 'e'. Among all letters that appear in both cases (E, T, C, O, D), E is the greatest alphabetically.
Example 2 — No Valid Letters
$ Input: s = "arRAzFif"
Output: "R"
💡 Note: The string contains both 'R' and 'r'. R is the only letter that appears in both uppercase and lowercase forms.
Example 3 — Single Letter Case
$ Input: s = "AbCdEfGhIjKlMnOpQrStUvWxYz"
Output: ""
💡 Note: Each letter appears only once in either uppercase or lowercase, but never both. No letter appears in both cases.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase and uppercase English letters only.

Visualization

Tap to expand
Greatest English Letter (Upper & Lower Case) INPUT String s: l E e T c O d E "lEeTcOdE" Legend: Uppercase Lowercase Uppercase found: E, T, O, E Lowercase found: l, e, c, d Input: s = "lEeTcOdE" ALGORITHM (Hash) 1 Create Hash Sets Store upper & lower chars upper: {E,T,O} lower: {l,e,c,d} 2 Iterate Z to A Check from greatest letter 3 Check Both Cases Upper AND lower exist? Letter Upper? Lower? Z-F NO -- E YES YES FOUND! 4 Return Result Return "E" (uppercase) FINAL RESULT Greatest letter with both cases: E Letter 'E' appears as: 'E' uppercase 'e' lowercase Output: "E" Key Insight: Use hash sets to store uppercase and lowercase characters separately. Then iterate from 'Z' to 'A' (greatest to smallest) and return the first letter found in BOTH sets. Time: O(n), Space: O(1). TutorialsPoint - Greatest English Letter in Upper and Lower Case | Hash Approach
Asked in
Google 15 Amazon 12 Microsoft 8
31.2K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen