-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
63 additions
and
1 deletion.
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,26 @@ | ||
// 统计出现过一次的公共字符串 | ||
// https://leetcode.cn/problems/count-common-words-with-one-occurrence | ||
// INLINE ../../images/array/count_common_words_with_one_occurrence.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
int countWords(vector<string> &words1, vector<string> &words2) { | ||
unordered_map<string, int> map1, map2; | ||
for (const string &w : words1) { | ||
++map1[w]; | ||
} | ||
for (const string &w : words2) { | ||
++map2[w]; | ||
} | ||
|
||
int res = 0; | ||
for (const auto &[w, cnt1] : map1) { | ||
if (cnt1 == 1 && map2[w] == 1) { | ||
++res; | ||
} | ||
} | ||
return res; | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
test/array/count_common_words_with_one_occurrence_test.cpp
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,32 @@ | ||
#include <array/count_common_words_with_one_occurrence.cpp> | ||
|
||
TEST(统计出现过一次的公共字符串, countWords) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:words1 = ["leetcode","is","amazing","as","is"], words2 = | ||
// ["amazing","leetcode","is"] 输出:2 解释: | ||
// - "leetcode" 在两个数组中都恰好出现一次,计入答案。 | ||
// - "amazing" 在两个数组中都恰好出现一次,计入答案。 | ||
// - "is" 在两个数组中都出现过,但在 words1 中出现了 2 次,不计入答案。 | ||
// - "as" 在 words1 中出现了一次,但是在 words2 中没有出现过,不计入答案。 | ||
// 所以,有 2 个字符串在两个数组中都恰好出现了一次。 | ||
vector<string> words1 = {"leetcode", "is", "amazing", "as", "is"}; | ||
vector<string> words2 = {"amazing", "leetcode", "is"}; | ||
EXPECT_EQ(solution.countWords(words1, words2), 2); | ||
|
||
// 示例 2: | ||
// 输入:words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"] | ||
// 输出:0 | ||
// 解释:没有字符串在两个数组中都恰好出现一次。 | ||
words1 = {"b", "bb", "bbb"}; | ||
words2 = {"a", "aa", "aaa"}; | ||
EXPECT_EQ(solution.countWords(words1, words2), 0); | ||
|
||
// 示例 3: | ||
// 输入:words1 = ["a","ab"], words2 = ["a","a","a","ab"] | ||
// 输出:1 | ||
// 解释:唯一在两个数组中都出现一次的字符串是 "ab" 。 | ||
words1 = {"a", "ab"}; | ||
words2 = {"a", "a", "a", "ab"}; | ||
EXPECT_EQ(solution.countWords(words1, words2), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-11 11:49:55 | ||
// 执行编译时间:2024-01-12 11:35:49 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|