From 320a7915dc7323c15fe0942d707e9793d7d5ae49 Mon Sep 17 00:00:00 2001 From: jusexton Date: Fri, 27 Dec 2024 02:20:21 -0600 Subject: [PATCH] Added ransom note solution. --- README.md | 3 ++- src/leetcode/mod.rs | 1 + src/leetcode/random_note.rs | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/leetcode/random_note.rs diff --git a/README.md b/README.md index 9b00199..0360e00 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,7 @@ programming challenges completed in other languages. - [Find Smallest Letter Greater than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target) - [Count Negative Numbers](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix) - [Guess Number](https://leetcode.com/problems/guess-number-higher-or-lower/) +- [Ransom Note](https://leetcode.com/problems/ransom-note) #### Medium @@ -252,7 +253,7 @@ programming challenges completed in other languages. - [Minimum Size SubArray](https://leetcode.com/problems/minimum-size-subarray-sum) - [Rotate Image](https://leetcode.com/problems/rotate-image) - [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes) - + #### Hard - [Kth Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance) diff --git a/src/leetcode/mod.rs b/src/leetcode/mod.rs index c350b65..dcad05a 100644 --- a/src/leetcode/mod.rs +++ b/src/leetcode/mod.rs @@ -103,6 +103,7 @@ mod prime_set_bits; mod prime_subtraction; mod product_except_self; mod product_sum_digits; +mod random_note; mod randomized_set; mod range_freq; mod range_sum_query; diff --git a/src/leetcode/random_note.rs b/src/leetcode/random_note.rs new file mode 100644 index 0000000..de8f1e1 --- /dev/null +++ b/src/leetcode/random_note.rs @@ -0,0 +1,20 @@ +pub fn can_construct(ransom_note: String, magazine: String) -> bool { + let mut characters = [0i32; 26]; + magazine.bytes().for_each(|b| { + characters[b as usize - 97] += 1; + }); + ransom_note.bytes().for_each(|b| { + characters[b as usize - 97] -= 1; + }); + characters.iter().all(|&x| x >= 0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn determines_if_ransom_note_can_be_constructed_from_magazine() { + assert!(can_construct("aa".to_string(), "aab".to_string())) + } +}