[LeetCode] Greatest English Letter in Upper and Lower Case

2309. Greatest English Letter in Upper and Lower Case

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string greatestLetter(string s) {
unordered_set<char> us(begin(s), end(s));
for(char ch = 'Z'; ch >= 'A'; ch--) {
if(us.count(ch) and us.count(ch-'A'+'a'))
return string(1,ch);
}

return "";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/19/PS/LeetCode/greatest-english-letter-in-upper-and-lower-case/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.