Skip to content

2309. Greatest English Letter in Upper and Lower Case 👍

  • Time: $O(n)$
  • Space: $O(128) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  string greatestLetter(string s) {
    vector<bool> seen(128);

    for (const char c : s)
      seen[c] = true;

    for (int i = 25; i >= 0; --i)
      if (seen['a' + i] && seen['A' + i])
        return string(1, 'A' + i);

    return "";
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public String greatestLetter(String s) {
    boolean[] seen = new boolean[128];

    for (final char c : s.toCharArray())
      seen[c] = true;

    for (int i = 25; i >= 0; --i)
      if (seen['a' + i] && seen['A' + i])
        return String.valueOf((char) ('A' + i));

    return "";
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def greatestLetter(self, s: str) -> str:
    seen = set(s)

    for i in range(25, -1, -1):
      if (chr(ord('a') + i) in seen and
              chr(ord('A') + i) in seen):
        return chr(ord('A') + i)

    return ''