-
Notifications
You must be signed in to change notification settings - Fork 10
/
find_common_characters.rs
52 lines (48 loc) · 1.68 KB
/
find_common_characters.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
/** https://leetcode.com/problems/find-common-characters/
需求: 求出一个单词数组中每个单词中都出现过的字母
由于输入仅由小写字母组成,很容易就想到用ASCII码的数组
为了命中CPU缓存,要每行表示一个小写字母,26行n列的矩阵,才能在计数完每个单词的字母出现次数后,逐行扫描统计字母出现次数的至少值
*/
#[allow(clippy::needless_range_loop)]
fn find_common_chars(a: Vec<String>) -> Vec<String> {
let n = a.len();
let mut arr = vec![vec![0_u8; n]; 26];
for word in 0..n {
for c in a[word].as_bytes() {
arr[(c - b'a') as usize][word] += 1;
}
}
let mut ret = Vec::new();
'outer: for letter in 0..26_usize {
let mut common_occur_times = 0;
for word in 0..n {
let letter_occur_times = arr[letter][word];
if letter_occur_times == 0 {
continue 'outer;
}
if common_occur_times == 0 {
common_occur_times = letter_occur_times;
} else {
common_occur_times = common_occur_times.min(letter_occur_times);
}
}
let letter_char = (letter as u8 + b'a') as char;
for _ in 0..common_occur_times {
ret.push(letter_char.to_string());
}
}
ret
}
#[test]
fn test_find_common_chars() {
let test_cases = vec![
(
vec_string!["bella", "label", "roller"],
vec_string!["e", "l", "l"],
),
(vec_string!["cool", "lock", "cook"], vec_string!["c", "o"]),
];
for (input, output) in test_cases {
assert_eq!(find_common_chars(input), output);
}
}