2309. Greatest English Letter in Upper and Lower Case Easy
1/**
2 * [2309] Greatest English Letter in Upper and Lower Case
3 *
4 * 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.
5 * An English letter b is greater than another letter a if b appears after a in the English alphabet.
6 *
7 * Example 1:
8 *
9 * Input: s = "l<u>Ee</u>TcOd<u>E</u>"
10 * Output: "E"
11 * Explanation:
12 * The letter 'E' is the only letter to appear in both lower and upper case.
13 *
14 * Example 2:
15 *
16 * Input: s = "a<u>rR</u>AzFif"
17 * Output: "R"
18 * Explanation:
19 * The letter 'R' is the greatest letter to appear in both lower and upper case.
20 * Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.
21 *
22 * Example 3:
23 *
24 * Input: s = "AbCdEfGhIjK"
25 * Output: ""
26 * Explanation:
27 * There is no letter that appears in both lower and upper case.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= s.length <= 1000
33 * s consists of lowercase and uppercase English letters.
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/
39// discuss: https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn greatest_letter(s: String) -> String {
45 let mut exist = vec![false; 58];
46 s.bytes().for_each(|b| exist[(b - b'A') as usize] = true);
47
48 for i in (0..26).rev() {
49 if exist[i] && exist[i + 32] {
50 return ((i as u8 + b'A') as char).to_string();
51 }
52 }
53 "".to_string()
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2309() {
65 assert_eq!(
66 Solution::greatest_letter("asdaerzSA".to_string()),
67 "S".to_owned()
68 );
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.