-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
59 lines (51 loc) · 1.37 KB
/
mod.rs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
pub struct Solution;
use std::cmp::Reverse;
impl Solution {
pub fn rank_teams(votes: Vec<String>) -> String {
let vec = votes
.iter()
.fold(vec![vec![0; votes[0].len()]; 26], |mut vec, str| {
str.bytes()
.map(|b| (b - b'A') as usize)
.enumerate()
.for_each(|(idx, b)| vec[b][idx] += 1);
vec
});
let mut result = votes[0].as_bytes().to_vec();
result.sort_unstable_by_key(|b| (Reverse(&vec[(*b - b'A') as usize]), *b));
String::from_utf8(result).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::ToVecString;
#[test]
fn test1() {
assert_eq!(
Solution::rank_teams(["ABC", "ACB", "ABC", "ACB", "ACB"].to_vec_string()),
"ACB"
);
}
#[test]
fn test2() {
assert_eq!(
Solution::rank_teams(["WXYZ", "XYZW"].to_vec_string()),
"XWYZ"
);
}
#[test]
fn test3() {
assert_eq!(
Solution::rank_teams(["ZMNAGUEDSJYLBOPHRQICWFXTVK"].to_vec_string()),
"ZMNAGUEDSJYLBOPHRQICWFXTVK"
);
}
#[test]
fn test4() {
assert_eq!(
Solution::rank_teams(["BCA", "CAB", "CBA", "ABC", "ACB", "BAC"].to_vec_string()),
"ABC"
);
}
}