-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/array/count_common_words_with_one_occurrence_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters