From 06fe666bcda63219c8ef6b14af3870d4798bf787 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Thu, 2 Jun 2022 23:43:57 +0100 Subject: [PATCH] ransom-note --- ransom-note/sol.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ransom-note/sol.rs diff --git a/ransom-note/sol.rs b/ransom-note/sol.rs new file mode 100644 index 0000000..71cbc66 --- /dev/null +++ b/ransom-note/sol.rs @@ -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 + }) + } +} \ No newline at end of file