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
| // @Title: 最长的美好子字符串 (Longest Nice Substring)
// @Author: 15816537946@163.com
// @Date: 2022-02-01 13:35:39
// @Runtime: 0 ms
// @Memory: 2 MB
impl Solution {
pub fn longest_nice_substring(s: String) -> String {
let sb = s.as_bytes();
let mut max_range: (usize, usize) = (0, 0);
for i in 0..(s.len() - 1) {
let (mut lower_mask, mut upper_mask) = (0u32, 0u32);
for j in i..s.len() {
match sb[j] >= b'a' {
true => lower_mask |= 1 << (sb[j] - b'a'),
false => upper_mask |= 1 << (sb[j] - b'A'),
};
if lower_mask == upper_mask && (j + 1 - i) > (max_range.1 - max_range.0) {
max_range = (i, j + 1);
}
}
}
String::from_utf8(sb[max_range.0..max_range.1].to_vec()).unwrap()
}
}
|