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