Skip to content

Commit

Permalink
add: 统计出现过一次的公共字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 12, 2024
1 parent ec7d67f commit 38f4b87
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [统计出现过一次的公共字符串](src/array/count_common_words_with_one_occurrence.rs) [数组, 哈希表, 字符串, 计数]

- LeetCode 2085. 统计出现过一次的公共字符串 <https://leetcode.cn/problems/count-common-words-with-one-occurrence>

- [回旋镖的数量](src/array/number_of_boomerangs.rs) [数组, 哈希表, 数学]

- LeetCode 447. 回旋镖的数量 <https://leetcode.cn/problems/number-of-boomerangs>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions src/array/count_common_words_with_one_occurrence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashMap;
// 统计出现过一次的公共字符串
// https://leetcode.cn/problems/count-common-words-with-one-occurrence
// INLINE ../../images/array/count_common_words_with_one_occurrence.jpeg

pub struct Solution;

impl Solution {
pub fn count_words(words1: Vec<String>, words2: Vec<String>) -> i32 {
let mut map1: HashMap<String, i32> = HashMap::new();
let mut map2: HashMap<String, i32> = HashMap::new();

for w in words1 {
*map1.entry(w).or_insert(0) += 1;
}

for w in words2 {
*map2.entry(w).or_insert(0) += 1;
}

let mut res = 0;
for (w, cnt1) in map1 {
if cnt1 == 1 && map2.get(&w) == Some(&1) {
res += 1;
}
}
res
}
}
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ pub mod stamping_the_grid;
pub mod min_cost_climbing_stairs;
pub mod maximum_profit_of_operating_a_centennial_wheel;
pub mod number_of_boomerangs;
pub mod count_common_words_with_one_occurrence;
51 changes: 51 additions & 0 deletions tests/array/count_common_words_with_one_occurrence_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use rust_practice::array::count_common_words_with_one_occurrence::Solution;

#[test]
fn count_words() {
// 示例 1:
// 输入:words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
// 输出:2
// 解释:
// - "leetcode" 在两个数组中都恰好出现一次,计入答案。
// - "amazing" 在两个数组中都恰好出现一次,计入答案。
// - "is" 在两个数组中都出现过,但在 words1 中出现了 2 次,不计入答案。
// - "as" 在 words1 中出现了一次,但是在 words2 中没有出现过,不计入答案。
// 所以,有 2 个字符串在两个数组中都恰好出现了一次。
let words1 = ["leetcode", "is", "amazing", "as", "is"]
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>();
let words2 = ["amazing", "leetcode", "is"]
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>();
assert_eq!(Solution::count_words(words1.to_vec(), words2.to_vec()), 2);

// 示例 2:
// 输入:words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"]
// 输出:0
// 解释:没有字符串在两个数组中都恰好出现一次。
let words1 = ["b", "bb", "bbb"]
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>();
let words2 = ["a", "aa", "aaa"]
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>();
assert_eq!(Solution::count_words(words1.to_vec(), words2.to_vec()), 0);

// 示例 3:
// 输入:words1 = ["a","ab"], words2 = ["a","a","a","ab"]
// 输出:1
// 解释:唯一在两个数组中都出现一次的字符串是 "ab" 。
let words1 = ["a", "ab"]
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>();
let words2 = ["a", "a", "a", "ab"]
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>();
assert_eq!(Solution::count_words(words1.to_vec(), words2.to_vec()), 1);
}
29 changes: 15 additions & 14 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
pub mod adding_two_negabinary_numbers_test;
pub mod apply_operations_to_an_array_test;
pub mod average_value_of_even_numbers_that_are_divisible_by_three_test;
pub mod check_distances_between_same_letters_test;
pub mod count_common_words_with_one_occurrence_test;
pub mod count_good_triplets_test;
pub mod count_vowel_strings_in_ranges_test;
pub mod determine_if_two_events_have_conflict_test;
pub mod equal_row_and_column_pairs_test;
pub mod filling_bookcase_shelves_test;
pub mod find_subarrays_with_equal_sum_test;
pub mod find_the_kth_smallest_sum_of_a_matrix_with_sorted_rows_test;
pub mod flip_columns_for_maximum_number_of_equal_rows_test;
pub mod group_anagrams_test;
pub mod house_robber_test;
pub mod kth_largest_element_in_an_array_test;
pub mod longest_arithmetic_subsequence_test;
pub mod longest_string_chain_test;
pub mod make_two_arrays_equal_by_reversing_sub_arrays_test;
pub mod maximum_fruits_harvested_after_at_most_k_steps_test;
pub mod maximum_length_of_pair_chain_test;
pub mod maximum_profit_of_operating_a_centennial_wheel_test;
pub mod maximum_size_subarray_sum_equals_k_test;
pub mod maximum_sum_of_two_non_overlapping_subarrays_test;
pub mod merge_intervals_test;
pub mod min_cost_climbing_stairs_test;
pub mod minimum_cost_to_merge_stones_test;
pub mod minimum_difficulty_of_a_job_schedule_test;
pub mod minimum_score_triangulation_of_polygon_test;
Expand All @@ -23,6 +32,10 @@ pub mod most_frequent_even_element_test;
pub mod move_zeroes_test;
pub mod non_overlapping_intervals_test;
pub mod number_of_arithmetic_triplets_test;
pub mod number_of_boomerangs_test;
pub mod number_of_times_binary_string_is_prefix_aligned_test;
pub mod number_of_unequal_triplets_in_array_test;
pub mod odd_string_difference_test;
pub mod online_majority_element_in_subarray_test;
pub mod pairs_of_songs_with_total_durations_divisible_by_60_test;
pub mod partition_array_for_maximum_sum_test;
Expand All @@ -35,22 +48,10 @@ pub mod reverse_subarray_to_maximize_array_value_test;
pub mod shuffle_the_array_test;
pub mod sliding_window_maximum_test;
pub mod special_positions_in_a_binary_matrix_test;
pub mod stamping_the_grid_test;
pub mod statistics_from_a_large_sample_test;
pub mod store_water_test;
pub mod teemo_attacking_test;
pub mod the_employee_that_worked_on_the_longest_task_test;
pub mod two_sum_test;
pub mod zero_matrix_lcci_test;
pub mod odd_string_difference_test;
pub mod statistics_from_a_large_sample_test;
pub mod find_the_kth_smallest_sum_of_a_matrix_with_sorted_rows_test;
pub mod average_value_of_even_numbers_that_are_divisible_by_three_test;
pub mod count_vowel_strings_in_ranges_test;
pub mod apply_operations_to_an_array_test;
pub mod equal_row_and_column_pairs_test;
pub mod number_of_unequal_triplets_in_array_test;
pub mod number_of_times_binary_string_is_prefix_aligned_test;
pub mod house_robber_test;
pub mod stamping_the_grid_test;
pub mod min_cost_climbing_stairs_test;
pub mod maximum_profit_of_operating_a_centennial_wheel_test;
pub mod number_of_boomerangs_test;

0 comments on commit 38f4b87

Please sign in to comment.