Greatest English Letter in Upper and Lower Case - Problem

You're given a string s containing English letters (both uppercase and lowercase). Your task is to find the greatest English letter that appears in both uppercase and lowercase forms within the string.

The "greatest" letter means the one that comes latest in the alphabet (e.g., 'Z' > 'Y' > 'X'). If you find such a letter, return it in uppercase. If no letter exists in both forms, return an empty string.

Example: In the string "lEeTcOdE", both 'E' and 'e' are present, so we return "E" (since E is the greatest qualifying letter).

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "lEeTcOdE"
โ€บ Output: "E"
๐Ÿ’ก Note: The string contains both 'E' and 'e'. While other letters like 'l', 'T', 'c', 'O', 'd' exist, they don't have both cases present. 'E' is the greatest letter that appears in both uppercase and lowercase.
example_2.py โ€” Multiple Pairs
$ Input: s = "arRAzFif"
โ€บ Output: "R"
๐Ÿ’ก Note: Both 'A'/'a' and 'R'/'r' appear in both cases. Since 'R' comes after 'A' in the alphabet, 'R' is the greatest letter that satisfies the condition.
example_3.py โ€” No Valid Pairs
$ Input: s = "AbCdEfGhIjK"
โ€บ Output: ""
๐Ÿ’ก Note: Each letter appears in only one case (either uppercase or lowercase), but never both. Therefore, no letter satisfies the condition and we return an empty string.

Visualization

Tap to expand
๐Ÿ•ต๏ธ Detective's Case: Find Greatest Letter PairEvidence: "lEeTcOdE"lWitnessEVIPeMATCH!๐Ÿ† Current BestEGreatest Valid Pair๐Ÿ” Investigation Process1. Examine each character as evidence2. Check if its partner (opposite case) was seen before3. If partnership confirmed, update VIP if this letter is greater
Understanding the Visualization
1
Scan the Evidence
Go through each character in the string, keeping track of what you've seen
2
Look for Pairs
When you see a character, check if you've already seen its opposite case
3
Track the VIP
Keep updating your answer to always point to the most important (greatest) valid pair
4
Present Findings
Return the most important witness, or empty string if no pairs exist
Key Takeaway
๐ŸŽฏ Key Insight: We don't need to search the entire alphabet - just process characters as we encounter them and immediately check for their case partner. This gives us O(n) time complexity with early detection of valid pairs!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, each character processed in O(1) time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Set can contain at most 52 characters (26 uppercase + 26 lowercase), which is constant space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s consists of lowercase and uppercase English letters only
  • Return the result in uppercase format
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~8 min Avg. Time
892 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