Greatest English Letter in Upper and Lower Case — LeetCode 2309 Python Solution
- Problem
- #2309
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
The 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.
Example
- Input
- s = "lEeTcOdE"
- Output
- "E"
- Explanation
- The letter 'E' is the only letter to appear in both lower and upper case.
Python solution
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 ''Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(C) auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2309. Greatest English Letter in Upper and Lower Case is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2309. Greatest English Letter in Upper and Lower Case?
- LeetCode 2309. Greatest English Letter in Upper and Lower Case is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2309. Greatest English Letter in Upper and Lower Case?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 2309. Greatest English Letter in Upper and Lower Case?
- The Python solution on this page uses O(C) auxiliary space.
- What topics does LeetCode 2309. Greatest English Letter in Upper and Lower Case cover?
- LeetCode 2309. Greatest English Letter in Upper and Lower Case is tagged Hash Table, String and Enumeration on LeetCode.