-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-letter-to-equalize-frequency.rs
72 lines (64 loc) · 2.56 KB
/
remove-letter-to-equalize-frequency.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
60
61
62
63
64
65
66
67
68
69
70
71
72
struct Solution;
impl Solution {
pub fn equal_frequency(word: String) -> bool {
use std::collections::{HashMap, HashSet};
let mut map: HashMap<char, u8> = HashMap::new();
for i in word.chars() {
match map.get_mut(&i) {
Some(value) => *value += 1,
None => {
map.insert(i, 1);
}
}
}
let mut v = map.into_values().collect::<Vec<u8>>();
v.sort();
v.reverse();
let len = v.len();
if len == 1
|| (len == 2 && v[len - 1] == 1)
|| (v[0] == v[len - 1] && v[0] == 1)
|| (v[len - 1] == 1 && v[..len - 1].iter().collect::<HashSet<&u8>>().len() == 1)
{
return true;
}
(v[1..].iter().collect::<HashSet<&u8>>().len() == 1) && v[0] - 1 == v[v.len() - 1]
}
}
// 1,1,2
fn main() {
println!("1 false == {}", Solution::equal_frequency("ddaccb".into()));
println!("2 false == {}", Solution::equal_frequency("babbdd".into()));
println!("3 false == {}", Solution::equal_frequency("aazz".into()));
println!("4 true == {}", Solution::equal_frequency("abcc".into()));
println!("5 true == {}", Solution::equal_frequency("abc".into()));
println!("6 true == {}", Solution::equal_frequency("abbcc".into()));
println!("7 true == {}", Solution::equal_frequency("cccaa".into()));
println!("8 false == {}", Solution::equal_frequency("ccaa".into()));
println!("9 true == {}", Solution::equal_frequency("ca".into()));
println!("10 true == {}", Solution::equal_frequency("zz".into()));
println!("11 true == {}", Solution::equal_frequency("cccd".into()));
println!(
"12 false == {}",
Solution::equal_frequency("aaaabbbbccc".into())
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert!(!Solution::equal_frequency("ddaccb".into()));
assert!(!Solution::equal_frequency("babbdd".into()));
assert!(!Solution::equal_frequency("aazz".into()));
assert!(Solution::equal_frequency("abcc".into()));
assert!(Solution::equal_frequency("abc".into()));
assert!(Solution::equal_frequency("abbcc".into()));
assert!(Solution::equal_frequency("cccaa".into()));
assert!(!Solution::equal_frequency("ccaa".into()));
assert!(Solution::equal_frequency("ca".into()));
assert!(Solution::equal_frequency("zz".into()));
assert!(Solution::equal_frequency("cccd".into()));
assert!(!Solution::equal_frequency("aaaabbbbccc".into()));
}
}