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.
Example 1:
Input: s = "lEeTcOdE" Output: "E" Explanation: The letter 'E' is the only letter to appear in both lower and upper case.
Example 2:
Input: s = "arRAzFif" Output: "R" Explanation: 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: "" Explanation: There is no letter that appears in both lower and upper case.
Constraints:
1 <= s.length <= 1000s consists of lowercase and uppercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem means we're going to check every single letter of the alphabet. We'll look to see if both its uppercase and lowercase versions appear in the input. We will remember the greatest letter that satisfies this requirement.
Here's how the algorithm would work step-by-step:
def greatest_letter(input_string: str) -> str:
for char_code in range(ord('Z'), ord('A') - 1, -1):
uppercase_letter = chr(char_code)
lowercase_letter = chr(char_code + 32)
# Check for both cases of the letter.
if uppercase_letter in input_string and lowercase_letter in input_string:
return uppercase_letter
# If we reach here, the current letter is not the answer.
# No matching letter found.
return ""To find the greatest English letter appearing in both upper and lower case, we want to efficiently check for pairs. Instead of looking at every letter, we'll use a trick to quickly see if a letter appears as both uppercase and lowercase.
Here's how the algorithm would work step-by-step:
def greatest_letter(input_string):
for char_code in range(ord('Z'), ord('A') - 1, -1):
uppercase_letter = chr(char_code)
lowercase_letter = chr(char_code + 32)
# Check if both upper and lowercase exist
if uppercase_letter in input_string and lowercase_letter in input_string:
# Return the greatest letter found
return uppercase_letter
# No matching letter found
return ""| Case | How to Handle |
|---|---|
| Empty input string | Return an empty string as there are no characters to evaluate. |
| Input string contains non-alphabetic characters | Filter out non-alphabetic characters or raise an exception to maintain expected input format. |
| Input string contains only uppercase letters | Return an empty string as no lowercase versions exist. |
| Input string contains only lowercase letters | Return an empty string as no uppercase versions exist. |
| Input string contains mixed-case letters, but no pair exists | Return an empty string as no valid solution exists. |
| Input string contains multiple valid letter pairs; find the largest | Iterate and keep track of the largest character found so far. |
| Input string contains only one character | Return an empty string because to qualify, the string needs to contain both upper and lower case of the same character. |
| Very long input string exceeding memory or causing performance issues | Use a memory efficient approach, like using a set to store the characters to prevent scalability issues. |