-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroup_anagrams.rs
59 lines (45 loc) · 1.28 KB
/
group_anagrams.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
use std::collections::HashMap;
pub fn input() -> Vec<String> {
vec![
// "eat".to_string(),
// "tea".to_string(),
// "tan".to_string(),
// "ate".to_string(),
// "nat".to_string(),
// "bat".to_string(),
"bdddddddddd".to_string(),
"bbbbbbbbbbc".to_string(),
]
}
pub fn another_solution(strs: Vec<String>) -> Vec<Vec<String>> {
let mut res = vec![];
let a_ascii = 'a' as u8;
let mut k: HashMap<String, Vec<String>> = HashMap::new();
for s in strs {
let count: [i32; 26] = [0; 26];
let mut count = count.to_vec();
for c in s.chars() {
count[(c as u8 - a_ascii) as usize] += 1;
}
let count : String = count.iter().map(|e| e.to_string()).collect();
if k.contains_key(&count) {
let v = k.get_mut(&count).unwrap();
v.push(s.to_string());
} else {
k.insert(count,vec![s]);
}
}
for a in k {
res.push(a.1);
}
res
}
pub fn solution(strs: Vec<String>) -> Vec<Vec<String>> {
let mut h = HashMap::new();
for s in strs {
let mut key: Vec<char> = s.chars().collect();
key.sort();
h.entry(key).or_insert(vec![]).push(s);
}
h.values().cloned().collect()
}