Skip to main content
Back to problems
#2309
Easy Algorithms

Greatest english letter in upper and lower case

Hash Table String Enumeration
71.9% acceptance
Feb 25, 2026
522
33
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn greatest_letter(s: String) -> String {
    let mut lower = [false; 26];
    let mut upper = [false; 26];
    for c in s.chars() {
      if c.is_lowercase() {
        lower[(c as u8 - b'a') as usize] = true;
      } else {
        upper[(c as u8 - b'A') as usize] = true;
      }
    }
    for i in (0..26).rev() {
      if lower[i] && upper[i] {
        return ((b'A' + i as u8) as char).to_string();
      }
    }
    String::new()
  }
}