20240505 Finished

This commit is contained in:
jackfiled 2024-05-05 11:57:17 +08:00
parent 2a2df06e67
commit 123c111329
2 changed files with 49 additions and 1 deletions

View File

@ -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;
mod p289_game_of_life;
mod p383_ransom_note;

View File

@ -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() {}
}