1
impl Solution {
2
pub fn largest_variance(s: String) -> i32 {
3
let au = 'a' as u8;
4
let s = s.into_bytes();
5
let counter = s.iter().fold(vec![0; 26], |mut vec, ch| {
6
vec[(ch - au) as usize] += 1;
7
vec
8
});
9

10
let mut g_max = 0;
11

12
for i in 0..26 {
13
for j in 0..26 {
14
if i == j || counter[i] == 0 || counter[j] == 0 {
15
continue;
16
}
17

18
let (maj, min) = (i as u8 + au, j as u8 + au);
19
let (mut maj_c, mut min_c) = (0, 0);
20
let mut min_r = counter[j];
21

22
s.iter().for_each(|c| {
23
match c {
24
x if *x == maj => {
25
maj_c += 1;
26
}
27
y if *y == min => {
28
min_c += 1;
29
min_r -= 1;
30
}
31
_ => (),
32
}
33

34
if min_c > 0 {
35
g_max = g_max.max(maj_c - min_c);
36
}
37

38
if maj_c < min_c && min_r > 0 {
39
maj_c = 0;
40
min_c = 0;
41
}
42
});
43
}
44
}
45

46
g_max
47
}
48
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0