##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2309.html

# 2309. Greatest English Letter in Upper and Lower Case

• Difficulty: Easy.
• Related Topics: Hash Table, String, Enumeration.
• Similar Questions: .

## 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. 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 <= 1000

• s consists of lowercase and uppercase English letters.

## Solution (Java, C++, Python)

• class Solution {
public String greatestLetter(String s) {
char gt = ' ';
boolean[] sA = new boolean[26];
boolean[] uA = new boolean[26];
for (char ch : s.toCharArray()) {
int i;
if (ch <= 'Z' && ch >= 'A') {
i = ch - 'A';
uA[i] = true;
} else {
i = ch - 'a';
sA[i] = true;
}
if (uA[i] == sA[i] && gt < ('A' + i)) {
gt = (char) ('A' + i);
}
}
return gt == ' ' ? "" : gt + "";
}
}

############

class Solution {
public String greatestLetter(String s) {
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (Character.isLowerCase(c)) {
mask1 |= 1 << (c - 'a');
} else {
mask2 |= 1 << (c - 'A');
}
}
: "";
}
}

• class Solution:
def greatestLetter(self, s: str) -> str:
for c in s:
if c.islower():
mask1 |= 1 << (ord(c) - ord("a"))
else:
mask2 |= 1 << (ord(c) - ord("A"))

############

# 2309. Greatest English Letter in Upper and Lower Case
# https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/

class Solution:
def greatestLetter(self, s: str) -> str:
s = set(s)

for j in range(25, -1, -1):
if chr(ord("a") + j) in s and chr(ord("A") + j) in s:
return chr(ord("A") + j)

return ""


• class Solution {
public:
string greatestLetter(string s) {
for (char& c : s) {
if (islower(c)) {
mask1 |= 1 << (c - 'a');
} else {
mask2 |= 1 << (c - 'A');
}
}
}
};

• func greatestLetter(s string) string {
for _, c := range s {
if unicode.IsLower(c) {
mask1 |= 1 << (c - 'a')
} else {
mask2 |= 1 << (c - 'A')
}
}
return ""
}
}

• 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 '';
}


• /**
* @param {string} s
* @return {string}
*/
var greatestLetter = function (s) {
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 '';
};


• 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()
}
}



Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).