Skip to content

Commit

Permalink
Update p383
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Jan 6, 2024
1 parent 4d95cdb commit dd57c7c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Call test by running `cargo test p[xxx]_`.

## 现在可以公开的情报(Problems Solved)

### Easy(66)
### Easy(67)

- [1. Two Sum](src/leetcode/p1_two_sum)
- [9. Palindrome Number](src/leetcode/p9_palindrome_number)
Expand All @@ -32,6 +32,7 @@ Call test by running `cargo test p[xxx]_`.
- [190. Reverse Bits](src/leetcode/p190_reverse_bits)
- [191. Number of 1 Bits](src/leetcode/p191_number_of_1_bits)
- [263. Ugly Number](src/leetcode/p263_ugly_number)
- [383. Ransom Note](src/leetcode/p383_ransom_note)
- [463. Island Perimeter](src/leetcode/p463_island_perimeter)
- [496. Next Greater Element I](src/leetcode/p496_next_greater_element_i)
- [705. Design HashSet](src/leetcode/p705_design_hashset)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub mod p354_russian_doll_envelopes;
pub mod p35_search_insert_position;
pub mod p36_valid_sudoku;
pub mod p37_sudoku_solver;
pub mod p383_ransom_note;
pub mod p38_count_and_say;
pub mod p39_combination_sum;
pub mod p3_longest_substring_without_repeating_characters;
Expand Down
39 changes: 39 additions & 0 deletions src/leetcode/p383_ransom_note/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub struct Solution;

impl Solution {
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
if magazine.len() < ransom_note.len() {
return false;
}

let mut count = [0; 26];
ransom_note
.bytes()
.for_each(|b| count[(b - b'a') as usize] += 1);
magazine
.bytes()
.for_each(|b| count[(b - b'a') as usize] -= 1);

count.iter().all(|&c| c <= 0)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test1() {
assert!(!Solution::can_construct("a".to_string(), "b".to_string()));
}

#[test]
fn test2() {
assert!(!Solution::can_construct("aa".to_string(), "ab".to_string()));
}

#[test]
fn test3() {
assert!(Solution::can_construct("aa".to_string(), "aab".to_string()));
}
}

0 comments on commit dd57c7c

Please sign in to comment.