LeetCode Problem Workspace
Greatest English Letter in Upper and Lower Case
Find the greatest English letter that exists in both lowercase and uppercase in a string using a hash table approach.
3
Topics
7
Code langs
3
Related
Practice Focus
Easy · Hash Table plus String
Answer-first summary
Find the greatest English letter that exists in both lowercase and uppercase in a string using a hash table approach.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Hash Table plus String
This problem asks to identify the largest English letter present in both uppercase and lowercase forms within a string. By iterating through the string and storing letters in sets, you can quickly check which letters appear in both cases. The final answer is returned in uppercase, or an empty string if no such letter exists.
Problem Statement
Given a string s consisting of English letters, return the greatest letter which appears in both lowercase and uppercase forms. The result must be uppercase, and return an empty string if no such letter exists.
An English letter b is considered greater than a if it comes later in the alphabet. For example, given s = "lEeTcOdE", the output should be "E" because it is the only letter appearing in both cases and no other letter is greater.
Examples
Example 1
Input: s = "lEeTcOdE"
Output: "E"
The letter 'E' is the only letter to appear in both lower and upper case.
Example 2
Input: s = "arRAzFif"
Output: "R"
The letter 'R' is the greatest letter to appear in both lower and upper case. Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.
Example 3
Input: s = "AbCdEfGhIjK"
Output: ""
There is no letter that appears in both lower and upper case.
Constraints
- 1 <= s.length <= 1000
- s consists of lowercase and uppercase English letters.
Solution Approach
Use Two Sets for Lowercase and Uppercase
Iterate through the string, adding each lowercase letter to one set and each uppercase letter to another. Then check for letters that exist in both sets and track the maximum letter.
Check Each Letter in Reverse Alphabet Order
Loop from 'Z' to 'A' and verify if both uppercase and lowercase versions exist in the respective sets. Return the first match found as it will be the greatest letter.
Return Uppercase or Empty String
If a common letter is found, return it in uppercase. If no letter exists in both cases, return an empty string.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) for scanning the string and O(1) for checking up to 26 letters. Space complexity is O(n) for storing sets of characters.
What Interviewers Usually Probe
- Look for a hash table or set-based solution instead of nested loops.
- Pay attention to the requirement that the result must be uppercase.
- Consider iterating the alphabet in reverse to find the greatest letter efficiently.
Common Pitfalls or Variants
Common pitfalls
- Returning a lowercase letter instead of the required uppercase result.
- Failing to check both cases properly when letters repeat in the string.
- Assuming alphabetical order from the string rather than using explicit letter comparison.
Follow-up variants
- Return all letters that appear in both cases in descending order.
- Find the smallest English letter present in both cases instead of the greatest.
- Handle strings with non-letter characters by filtering out invalid characters before processing.
FAQ
What is the main pattern used in Greatest English Letter in Upper and Lower Case?
The problem uses a hash table plus string pattern where sets track lowercase and uppercase letters for fast lookup.
How do I return the correct letter format?
Always return the letter in uppercase regardless of how it appears in the original string.
What if no letter exists in both cases?
Return an empty string to indicate no letter satisfies the condition.
Can this solution handle long strings efficiently?
Yes, storing letters in sets ensures O(n) time complexity, even for strings up to 1000 characters.
Why iterate letters in reverse order?
Checking from 'Z' to 'A' ensures the first common letter found is the greatest, satisfying the problem requirement.
Solution
Solution 1: Hash Table + Enumeration
First, we use a hash table $ss$ to record all the letters that appear in the string $s$. Then we start enumerating from the last letter of the uppercase alphabet. If both the uppercase and lowercase forms of the current letter are in $ss$, we return that letter.
class Solution:
def greatestLetter(self, s: str) -> str:
ss = set(s)
for c in ascii_uppercase[::-1]:
if c in ss and c.lower() in ss:
return c
return ''Solution 2: Bit Manipulation (Space Optimization)
We can use two integers $mask1$ and $mask2$ to record the lowercase and uppercase letters that appear in the string $s$, respectively. The $i$-th bit of $mask1$ indicates whether the $i$-th lowercase letter appears, and the $i$-th bit of $mask2$ indicates whether the $i$-th uppercase letter appears.
class Solution:
def greatestLetter(self, s: str) -> str:
ss = set(s)
for c in ascii_uppercase[::-1]:
if c in ss and c.lower() in ss:
return c
return ''Continue Topic
hash table
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Hash Table plus String
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward