From 123c1113291c051ff61149fd40cefd52e8461b6d Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 5 May 2024 11:57:17 +0800 Subject: [PATCH] 20240505 Finished --- src/problem/mod.rs | 3 ++- src/problem/p383_ransom_note.rs | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/problem/p383_ransom_note.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 5991f0b..002ed57 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -121,4 +121,5 @@ mod p36_valid_sudoku; mod p54_spiral_matrix; mod p48_rotate_image; mod p73_set_matrix_zeroes; -mod p289_game_of_life; \ No newline at end of file +mod p289_game_of_life; +mod p383_ransom_note; \ No newline at end of file diff --git a/src/problem/p383_ransom_note.rs b/src/problem/p383_ransom_note.rs new file mode 100644 index 0000000..dab850b --- /dev/null +++ b/src/problem/p383_ransom_note.rs @@ -0,0 +1,47 @@ +/** + * [383] Ransom Note + */ +pub struct Solution {} + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn can_construct(ransom_note: String, magazine: String) -> bool { + let mut map = HashMap::new(); + + for c in magazine.chars() { + let entry = map.entry(c).or_insert(0); + + *entry += 1; + } + + for c in ransom_note.chars() { + match map.get_mut(&c) { + Some(entry) => { + if *entry == 0 { + return false; + } + + *entry -= 1; + }, + None => { + return false; + } + } + } + + + true + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_383() {} +}