ransom-note

This commit is contained in:
Gleb Koval 2022-06-02 23:43:57 +01:00
parent 32c5f971c6
commit 06fe666bcd
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 20 additions and 0 deletions

20
ransom-note/sol.rs Normal file
View File

@ -0,0 +1,20 @@
use std::collections::HashMap;
impl Solution {
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
let mut magazine_map = magazine.bytes()
.fold([0; 26], |mut map, chr| {
map[chr as usize - 97] += 1;
map
});
ransom_note.bytes()
.all(|chr| {
let old = magazine_map[chr as usize - 97];
if old == 0 {
return false;
}
magazine_map[chr as usize - 97] = old - 1;
true
})
}
}