Problem Description
Given a string of English letters, return the greatest English letter (in uppercase) that appears in both uppercase and lowercase form in the string. If no such letter exists, return an empty string.
Key Insights
- Use two sets (or equivalent) to record the occurrence of uppercase and lowercase letters found in the string.
- Iterate over the characters to fill the corresponding sets.
- Then check from 'Z' down to 'A' to determine the highest letter that appears in both sets.
- Since the English alphabet is fixed, iterating over letters is constant time.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string. Space Complexity: O(1) (or constant space) since the maximum number of stored elements is limited to 26 letters for each case.
Solution
We first traverse the input string once to populate two sets: one for lowercase letters and one for uppercase letters. After that, we iterate from 'Z' down to 'A' and check whether each letter's lowercase variant (using a conversion) is present in the lowercase set. The first letter found that meets this condition is the answer because we are iterating from the greatest letter (by alphabetical order) downward. If no letter is found that exists in both sets, we return an empty string.