Given a string of English letters s, return thegreatest English letter which occurs as both a lowercase and uppercase letter ins. 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
aftera in the English alphabet.
Input: s ="a** _rR_** AzFif"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'.
We want the greatest English letter that appears in both lowercase and uppercase in the string. By collecting all lowercase and uppercase letters, we can check from ‘Z’ to ‘A’ if both cases exist for a letter.
classSolution {
public String greatestLetter(String s) {
boolean[] lower =newboolean[26], upper =newboolean[26];
for (char c : s.toCharArray()) {
if (c >='a'&& c <='z') lower[c -'a']=true;
if (c >='A'&& c <='Z') upper[c -'A']=true;
}
for (char c ='Z'; c >='A'; c--) {
if (upper[c -'A']&& lower[c -'A']) return String.valueOf(c);
}
return"";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
fungreatestLetter(s: String): String {
val lower = BooleanArray(26)
val upper = BooleanArray(26)
for (c in s) {
if (c in'a'..'z') lower[c - 'a'] = trueif (c in'A'..'Z') upper[c - 'A'] = true }
for (c in'Z' downTo 'A') {
if (upper[c - 'A'] && lower[c - 'A']) return c.toString()
}
return"" }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defgreatestLetter(self, s: str) -> str:
lower = set()
upper = set()
for c in s:
if c.islower():
lower.add(c)
elif c.isupper():
upper.add(c)
for c in reversed('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
if c in upper and c.lower() in lower:
return c
return""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfngreatest_letter(s: String) -> String {
letmut lower = [false; 26];
letmut upper = [false; 26];
for b in s.bytes() {
if b >=b'a'&& b <=b'z' {
lower[(b -b'a') asusize] =true;
} elseif b >=b'A'&& b <=b'Z' {
upper[(b -b'A') asusize] =true;
}
}
for i in (0..26).rev() {
if lower[i] && upper[i] {
return ((b'A'+ i asu8) aschar).to_string();
}
}
String::new()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
greatestLetter(s: string):string {
constlower=newSet<string>(), upper=newSet<string>();
for (constcofs) {
if (c>='a'&&c<='z') lower.add(c);
if (c>='A'&&c<='Z') upper.add(c);
}
for (leti=25; i>=0; i--) {
constup= String.fromCharCode(65+i), lo= String.fromCharCode(97+i);
if (upper.has(up) &&lower.has(lo)) returnup;
}
return"";
}
}