Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"lEeTcOdE"
"arRAzFif"
"AbCdEfGhIjK"
count-the-number-of-special-characters-ii)count-the-number-of-special-characters-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2309: Greatest English Letter in Upper and Lower Case
class Solution {
public String greatestLetter(String s) {
Set<Character> ss = new HashSet<>();
for (char c : s.toCharArray()) {
ss.add(c);
}
for (char a = 'Z'; a >= 'A'; --a) {
if (ss.contains(a) && ss.contains((char) (a + 32))) {
return String.valueOf(a);
}
}
return "";
}
}
// Accepted solution for LeetCode #2309: Greatest English Letter in Upper and Lower Case
func greatestLetter(s string) string {
ss := map[rune]bool{}
for _, c := range s {
ss[c] = true
}
for c := 'Z'; c >= 'A'; c-- {
if ss[c] && ss[rune(c+32)] {
return string(c)
}
}
return ""
}
# Accepted solution for LeetCode #2309: Greatest English Letter in Upper and Lower Case
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 ''
// Accepted solution for LeetCode #2309: Greatest English Letter in Upper and Lower Case
impl Solution {
pub fn greatest_letter(s: String) -> String {
let mut arr = [0; 26];
for &c in s.as_bytes().iter() {
if c >= b'a' {
arr[(c - b'a') as usize] |= 1;
} else {
arr[(c - b'A') as usize] |= 2;
}
}
for i in (0..26).rev() {
if arr[i] == 3 {
return char::from(b'A' + (i as u8)).to_string();
}
}
"".to_string()
}
}
// Accepted solution for LeetCode #2309: Greatest English Letter in Upper and Lower Case
function greatestLetter(s: string): string {
const ss = new Array(128).fill(false);
for (const c of s) {
ss[c.charCodeAt(0)] = true;
}
for (let i = 90; i >= 65; --i) {
if (ss[i] && ss[i + 32]) {
return String.fromCharCode(i);
}
}
return '';
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.