forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
31 lines (31 loc) · 895 Bytes
/
Solution.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
impl Solution {
pub fn shortest_completing_word(license_plate: String, words: Vec<String>) -> String {
let mut cnt = vec![0; 26];
for c in license_plate.chars() {
if c.is_ascii_alphabetic() {
cnt[((c.to_ascii_lowercase() as u8) - b'a') as usize] += 1;
}
}
let mut ans = String::new();
for w in words {
if !ans.is_empty() && w.len() >= ans.len() {
continue;
}
let mut t = vec![0; 26];
for c in w.chars() {
t[((c as u8) - b'a') as usize] += 1;
}
let mut ok = true;
for i in 0..26 {
if t[i] < cnt[i] {
ok = false;
break;
}
}
if ok {
ans = w;
}
}
ans
}
}