-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2273.rs
41 lines (37 loc) · 1.17 KB
/
p2273.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
#[test]
fn test() {
let words1: Vec<String> = vec!["abba", "baba", "bbaa", "cd", "cd"]
.iter()
.map(|s| s.to_string())
.collect();
let ans1: Vec<String> = vec!["abba", "cd"].iter().map(|s| s.to_string()).collect();
assert_eq!(remove_anagrams(words1), ans1);
let words2: Vec<String> = vec!["a", "b", "c", "d", "e"]
.iter()
.map(|s| s.to_string())
.collect();
let ans2: Vec<String> = vec!["a", "b", "c", "d", "e"]
.iter()
.map(|s| s.to_string())
.collect();
assert_eq!(remove_anagrams(words2), ans2);
}
pub fn remove_anagrams(words: Vec<String>) -> Vec<String> {
// 栈
let mut stack: Vec<String> = vec![];
for word in words {
// hashmap array
let mut cnt: [i32; 26] = [0; 26];
for ch in word.chars() {
cnt[ch as usize - 'a' as usize] += 1;
}
for ch in stack.last().unwrap_or(&"".to_string()).chars() {
cnt[ch as usize - 'a' as usize] -= 1;
}
// 用hashmap检查是否是字母异位词,如果不是字母异位词,入栈
if cnt != [0; 26] {
stack.push(word);
}
}
stack
}