2309. Greatest English Letter in Upper and Lower Case LeetCode Solution

In this guide, you will get 2309. Greatest English Letter in Upper and Lower Case LeetCode Solution with the best time and space complexity. The solution to Greatest English Letter in Upper and Lower Case problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Greatest English Letter in Upper and Lower Case solution in C++
  4. Greatest English Letter in Upper and Lower Case solution in Java
  5. Greatest English Letter in Upper and Lower Case solution in Python
  6. Additional Resources
2309. Greatest English Letter in Upper and Lower Case LeetCode Solution image

Problem Statement of 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.

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.

See also  1938. Maximum Genetic Difference Query LeetCode Solution

Constraints:

1 <= s.length <= 1000
s consists of lowercase and uppercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(128) = O(1)

2309. Greatest English Letter in Upper and Lower Case LeetCode Solution in C++

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 "";
  }
};
/* code provided by PROGIEZ */

2309. Greatest English Letter in Upper and Lower Case LeetCode Solution in Java

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 "";
  }
}
// code provided by PROGIEZ

2309. Greatest English Letter in Upper and Lower Case LeetCode Solution in Python

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 ''
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.