LeetCode 2309: Greatest English Letter in Upper and Lower Case Solution
Master LeetCode problem 2309 (Greatest English Letter in Upper and Lower Case), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2309. Greatest English Letter in Upper and Lower Case
Problem Explanation
Explanation:
To solve this problem, we can iterate through the given string s and maintain a set of lowercase and uppercase letters that we have encountered. For each letter, we check if its uppercase and lowercase versions are both present in the corresponding sets. If they are, we update the result letter if the current letter is greater than the previous result letter. Finally, we return the result letter in uppercase.
- Initialize an empty set for lowercase letters and uppercase letters.
- Initialize a variable
resultas an empty string. - Iterate through each character in the input string
s. - If the character is a lowercase letter, add it to the lowercase set.
- If the character is an uppercase letter, add it to the uppercase set.
- For each letter in the lowercase set, check if its uppercase version is also in the uppercase set.
- If they are both present, update the
resultletter if the current letter is greater than the previous result letter. - Return the
resultletter in uppercase if it exists, otherwise, return an empty string.
Time Complexity:
The time complexity of this algorithm is O(n), where n is the length of the input string s.
Space Complexity:
The space complexity of this algorithm is O(1) since we are using a constant amount of extra space for storing the sets and result letter.
:
Solution Code
class Solution {
public String greatestLetter(String s) {
Set<Character> lowercaseSet = new HashSet<>();
Set<Character> uppercaseSet = new HashSet<>();
String result = "";
for (char c : s.toCharArray()) {
if (Character.isLowerCase(c)) {
lowercaseSet.add(c);
} else if (Character.isUpperCase(c)) {
uppercaseSet.add(c);
}
}
for (char c : lowercaseSet) {
if (uppercaseSet.contains(Character.toUpperCase(c))) {
if (result.isEmpty() || Character.toUpperCase(c) > result.charAt(0)) {
result = String.valueOf(Character.toUpperCase(c));
}
}
}
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2309 (Greatest English Letter in Upper and Lower Case)?
This page provides optimized solutions for LeetCode problem 2309 (Greatest English Letter in Upper and Lower Case) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2309 (Greatest English Letter in Upper and Lower Case)?
The time complexity for LeetCode 2309 (Greatest English Letter in Upper and Lower Case) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2309 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2309 in Java, C++, or Python.