From aedfa99c553734b095af2c3a6b829addfe9f8aa7 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 12 Jan 2024 15:21:03 +0800 Subject: [PATCH] 20240112 Finished --- src/solution/mod.rs | 1 + ..._count_common_words_with_one_occurrence.rs | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/solution/s2085_count_common_words_with_one_occurrence.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 854953f..e2e4d33 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -14,3 +14,4 @@ mod s0007_reverse_integer; mod s0004_median_of_two_sorted_arrays; mod s0743_network_delay_time; mod s0447_number_of_boomerangs; +mod s2085_count_common_words_with_one_occurrence; diff --git a/src/solution/s2085_count_common_words_with_one_occurrence.rs b/src/solution/s2085_count_common_words_with_one_occurrence.rs new file mode 100644 index 0000000..1d3ee2d --- /dev/null +++ b/src/solution/s2085_count_common_words_with_one_occurrence.rs @@ -0,0 +1,86 @@ +/** + * [2085] Count Common Words With One Occurrence + * + * Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays. + * + * Example 1: + * + * Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"] + * Output: 2 + * Explanation: + * - "leetcode" appears exactly once in each of the two arrays. We count this string. + * - "amazing" appears exactly once in each of the two arrays. We count this string. + * - "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string. + * - "as" appears once in words1, but does not appear in words2. We do not count this string. + * Thus, there are 2 strings that appear exactly once in each of the two arrays. + * + * Example 2: + * + * Input: words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"] + * Output: 0 + * Explanation: There are no strings that appear in each of the two arrays. + * + * Example 3: + * + * Input: words1 = ["a","ab"], words2 = ["a","a","a","ab"] + * Output: 1 + * Explanation: The only string that appears exactly once in each of the two arrays is "ab". + * + * + * Constraints: + * + * 1 <= words1.length, words2.length <= 1000 + * 1 <= words1[i].length, words2[j].length <= 30 + * words1[i] and words2[j] consists only of lowercase English letters. + * + */ +pub struct Solution {} + +// problem: https://leetcode.cn/problems/count-common-words-with-one-occurrence/ +// discuss: https://leetcode.cn/problems/count-common-words-with-one-occurrence/discuss/?currentPage=1&orderBy=most_votes&query= + +// submission codes start here + +use std::collections::HashMap; + +impl Solution { + pub fn count_words(words1: Vec, words2: Vec) -> i32 { + let mut dict = HashMap::new(); + + for i in &words1 { + match dict.get(i) { + None => { + dict.insert(i, 0);} + Some(_) => { + dict.insert(i ,1); + } + } + } + + for i in &words2 { + match dict.get(i) { + None => {} + Some(value) => { + if *value == 0 { + dict.insert(i, 2); + } else if *value == 2{ + dict.insert(i, 1); + } + } + } + } + + dict.values().filter(|x| {**x == 2}).count() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2085() { + } +}