class Solution {
public:
int countWords(vector<string>& words1, vector<string>& words2) {
}
};
2085. 统计出现过一次的公共字符串
给你两个字符串数组 words1
和 words2
,请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。
示例 1:
输入:words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"] 输出:2 解释: - "leetcode" 在两个数组中都恰好出现一次,计入答案。 - "amazing" 在两个数组中都恰好出现一次,计入答案。 - "is" 在两个数组中都出现过,但在 words1 中出现了 2 次,不计入答案。 - "as" 在 words1 中出现了一次,但是在 words2 中没有出现过,不计入答案。 所以,有 2 个字符串在两个数组中都恰好出现了一次。
示例 2:
输入:words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"] 输出:0 解释:没有字符串在两个数组中都恰好出现一次。
示例 3:
输入:words1 = ["a","ab"], words2 = ["a","a","a","ab"] 输出:1 解释:唯一在两个数组中都出现一次的字符串是 "ab" 。
提示:
1 <= words1.length, words2.length <= 1000
1 <= words1[i].length, words2[j].length <= 30
words1[i]
和 words2[j]
都只包含小写英文字母。原站题解
javascript 解法, 执行用时: 68 ms, 内存消耗: 52 MB, 提交时间: 2024-01-12 00:03:44
/** * @param {string[]} words1 * @param {string[]} words2 * @return {number} */ var countWords = function(words1, words2) { // 统计字符串出现频率 const freq1 = new Map(); const freq2 = new Map(); for (const w of words1) { freq1.set(w, (freq1.get(w) || 0) + 1); } for (const w of words2) { freq2.set(w, (freq2.get(w) || 0) + 1); } // 遍历 words1 出现的字符串并检查个数 let res = 0; for (const [w, cnt1] of freq1.entries()) { if (cnt1 === 1 && freq2.get(w) === 1) { res++; } } return res; };
java 解法, 执行用时: 9 ms, 内存消耗: 43.3 MB, 提交时间: 2024-01-12 00:03:29
class Solution { public int countWords(String[] words1, String[] words2) { // 统计字符串出现频率 Map<String, Integer> freq1 = new HashMap<>(); Map<String, Integer> freq2 = new HashMap<>(); for (String w : words1) { freq1.put(w, freq1.getOrDefault(w, 0) + 1); } for (String w : words2) { freq2.put(w, freq2.getOrDefault(w, 0) + 1); } // 遍历 words1 出现的字符并判断是否满足要求 int res = 0; for (String w : freq1.keySet()) { if (freq1.get(w) == 1 && freq2.getOrDefault(w, 0) == 1) { res++; } } return res; } }
cpp 解法, 执行用时: 32 ms, 内存消耗: 18.7 MB, 提交时间: 2024-01-12 00:03:15
class Solution { public: int countWords(vector<string>& words1, vector<string>& words2) { // 统计字符串出现频率 unordered_map<string, int> freq1, freq2; for (const string& w: words1) { ++freq1[w]; } for (const string& w: words2) { ++freq2[w]; } // 遍历 words1 出现的字符并判断是否满足要求 int res = 0; for (const auto& [w, cnt1] : freq1) { if (cnt1 == 1 && freq2[w] == 1) { ++res; } } return res; } };
golang 解法, 执行用时: 8 ms, 内存消耗: 6.3 MB, 提交时间: 2024-01-12 00:02:49
func countWords(words1, words2 []string) (ans int) { cnt1 := map[string]int{} cnt2 := map[string]int{} for _, s := range words1 { cnt1[s]++ } // 统计单词出现次数 for _, s := range words2 { cnt2[s]++ } // 统计单词出现次数 for _, s := range words2 { if cnt1[s] == 1 && cnt2[s] == 1 { ans++ }} // 单词都恰好出现一次 return }
python3 解法, 执行用时: 36 ms, 内存消耗: 15.5 MB, 提交时间: 2022-05-28 22:39:58
class Solution: def countWords(self, words1: List[str], words2: List[str]) -> int: m1, m2 = Counter(words1), Counter(words2) res = 0 for k1 in m1.keys(): if m1[k1] == 1 and m2[k1] == 1: res += 1 return res
python3 解法, 执行用时: 68 ms, 内存消耗: 15.6 MB, 提交时间: 2022-05-28 22:38:34
class Solution: def countWords(self, words1: List[str], words2: List[str]) -> int: m1, m2 = Counter(words1), Counter(words2) k1, k2 = [x for x, y in m1.items() if y==1], [x for x, y in m2.items() if y==1] return len([w for w in k1 if w in k2])