Skip to content

Commit

Permalink
Added ransom note solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Dec 27, 2024
1 parent e9f04a8 commit 320a791
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/leetcode/random_note.rs
Original file line number Diff line number Diff line change
@@ -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()))
}
}

0 comments on commit 320a791

Please sign in to comment.