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: ""
💡 Note: The string contains 'R' and 'r', but no other letter appears in both cases. R is not the greatest possible, but it's the only valid one. Wait, let me recheck - actually 'R' and 'r' are both present, so the answer should be "R".
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 in Upper and Lower CaseFind the greatest letter that appears in both uppercase and lowercaseInput String:"lEeTcOdE"Characters Found:Upper: E, T, OLower: l, e, c, dBoth: E/e, T/t, O/o, C/c, D/dCheck Z→A:Z/z: ✗ Not bothY/y: ✗ Not both...E/e: ✓ First match!Valid letters with both cases: E, T, C, O, DGreatest alphabetically: E (comes first in Z→A check)Result: "E"
Understanding the Visualization
1
Input Analysis
Scan string for all characters
2
Find Valid Pairs
Identify letters with both cases
3
Return Greatest
Pick alphabetically largest valid letter
Key Takeaway
🎯 Key Insight: We need to find the greatest letter that exists in both uppercase and lowercase forms in the string.
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