Skip to content

Commit 7270c6c

Browse files
solves #2309: Greatest English Letter in Upper and Lower Case in java
1 parent 9566fad commit 7270c6c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@
742742
| 2293 | [Min Max Game](https://leetcode.com/problems/min-max-game) | [![Java](assets/java.png)](src/MinMaxGame.java) | |
743743
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii) | [![Java](assets/java.png)](src/StrongPasswordCheckerII.java) | |
744744
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
745-
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes) | [![Java](assets/java.png)](src/CalculateAmountPaidInTaxes.java) [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
745+
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes) | [![Java](assets/java.png)](src/CalculateAmountPaidInTaxes.java) | |
746+
| 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case) | [![Java](assets/java.png)](src/GreatestEnglishLetterInUpperAndLowerCase.java) | |
746747
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
747748
| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
748749
| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |

Diff for: src/GreatestEnglishLetterInUpperAndLowerCase.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case
2+
// T: O(|s|)
3+
// S: O(1)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class GreatestEnglishLetterInUpperAndLowerCase {
9+
public String greatestLetter(String s) {
10+
final Set<Character> lowercaseLetters = getLowercaseLetters(s);
11+
final Set<Character> uppercaseLetters = getUppercaseLetters(s);
12+
char maxLetter = 'a' - 1;
13+
14+
for (char letter : lowercaseLetters) {
15+
if (uppercaseLetters.contains(Character.toUpperCase(letter)) && letter > maxLetter) {
16+
maxLetter = letter;
17+
}
18+
}
19+
20+
return maxLetter >= 'a' ? (Character.toUpperCase(maxLetter) + "") : "";
21+
}
22+
23+
private Set<Character> getUppercaseLetters(String string) {
24+
final Set<Character> set = new HashSet<>();
25+
for (int i = 0 ; i < string.length() ; i++) {
26+
if (Character.isUpperCase(string.charAt(i))) {
27+
set.add(string.charAt(i));
28+
}
29+
}
30+
return set;
31+
}
32+
33+
private Set<Character> getLowercaseLetters(String string) {
34+
final Set<Character> set = new HashSet<>();
35+
for (int i = 0 ; i < string.length() ; i++) {
36+
if (Character.isLowerCase(string.charAt(i))) {
37+
set.add(string.charAt(i));
38+
}
39+
}
40+
return set;
41+
}
42+
}

0 commit comments

Comments
 (0)