-
Notifications
You must be signed in to change notification settings - Fork 9
/
anagrams.rs
47 lines (45 loc) · 1.55 KB
/
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
/// https://leetcode.com/problems/valid-anagram/
fn is_anagram(s: String, t: String) -> bool {
if s.len() != t.len() {
return false;
}
let mut counter = [0_u16; 26];
for each in s.into_bytes() {
counter[(each - b'a') as usize] += 1;
}
for each in t.into_bytes() {
let idx = (each - b'a') as usize;
if counter[idx] == 0 {
return false;
}
counter[idx] -= 1;
}
true
}
/// https://leetcode.com/problems/group-anagrams/
/// 由于Python没有原始数组,list是可变的不能Hash,所以list要转为tuple多了很多额外的操作
fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
let mut group = std::collections::HashMap::new();
for s in strs {
let mut counter = [0_u8; 26];
for &byte in s.as_bytes() {
counter[(byte - b'a') as usize] += 1;
}
group.entry(counter).or_insert_with(Vec::new).push(s);
}
group.into_values().collect()
}
/// https://leetcode.com/problems/find-anagram-mappings/
/// 有两个互为anagram的数组(anagram的定义请看valid_anagram一题)
/// 请你找出A的每个元素在B中的索引,如有重复元素则返回任意一个索引
/// 例如A=[12,28],B=[28,12],因为A[0]=B[1],A[1]=B[0],所以返回[1,0]
fn anagram_mappings(mut a: Vec<i32>, b: Vec<i32>) -> Vec<i32> {
let mut map = std::collections::HashMap::with_capacity(b.len());
for (i, b) in b.into_iter().enumerate() {
map.insert(b, i as i32);
}
for a in &mut a {
*a = map[a];
}
a
}