Greatest English Letter in Upper and Lower Case — LeetCode 2309 Python Solution

EasyHash TableStringEnumeration
Problem
#2309
Pattern
Hash Map
Reading time
2 min

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

Python
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

MeasureComplexity
TimeO(n)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview