Skip to content

Commit 647398a

Browse files
committed
src/bin/greatest-english-letter-in-upper-and-lower-case.rs
1 parent 81d94a2 commit 647398a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![allow(dead_code, unused, unused_variables, non_snake_case)]
2+
3+
use std::cmp::max;
4+
5+
fn main() {}
6+
7+
struct Solution;
8+
9+
impl Solution {
10+
pub fn greatest_letter(s: String) -> String {
11+
let mut r = "".to_owned();
12+
let mut v = vec![0u8; 26]; // 0代表没有,1代表小写,2代表大写
13+
14+
for i in 0..s.as_bytes().len() {
15+
let m = s.as_bytes()[i];
16+
match m {
17+
b'A'..=b'Z' => {
18+
if v[(m - b'A') as usize] == 1 {
19+
r = r.max(s.as_str()[i..i + 1].to_string());
20+
v[(m - b'A') as usize] = 3;
21+
} else {
22+
v[(m - b'A') as usize] = 2;
23+
}
24+
}
25+
b'a'..=b'z' => {
26+
if v[(m - b'a') as usize] == 2 {
27+
r = r.max(s.as_str()[i..i + 1].to_string().to_uppercase());
28+
v[(m - b'a') as usize] = 3;
29+
} else {
30+
v[(m - b'a') as usize] = 1;
31+
}
32+
}
33+
_ => unreachable!(),
34+
}
35+
}
36+
37+
r
38+
}
39+
}

0 commit comments

Comments
 (0)