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