返回介绍

solution / 2000-2099 / 2085.Count Common Words With One Occurrence / README_EN

发布于 2024-06-17 01:03:11 字数 5162 浏览 0 评论 0 收藏 0

2085. Count Common Words With One Occurrence

中文文档

Description

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.

Solutions

Solution 1: Hash Table + Counting

We can use two hash tables, $cnt1$ and $cnt2$, to count the occurrences of each string in the two string arrays respectively. Then, we traverse one of the hash tables. If a string appears once in the other hash table and also appears once in the current hash table, we increment the answer by one.

The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of the two string arrays respectively.

class Solution:
  def countWords(self, words1: List[str], words2: List[str]) -> int:
    cnt1 = Counter(words1)
    cnt2 = Counter(words2)
    return sum(v == 1 and cnt2[w] == 1 for w, v in cnt1.items())
class Solution {
  public int countWords(String[] words1, String[] words2) {
    Map<String, Integer> cnt1 = new HashMap<>();
    Map<String, Integer> cnt2 = new HashMap<>();
    for (var w : words1) {
      cnt1.merge(w, 1, Integer::sum);
    }
    for (var w : words2) {
      cnt2.merge(w, 1, Integer::sum);
    }
    int ans = 0;
    for (var e : cnt1.entrySet()) {
      if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countWords(vector<string>& words1, vector<string>& words2) {
    unordered_map<string, int> cnt1;
    unordered_map<string, int> cnt2;
    for (auto& w : words1) {
      ++cnt1[w];
    }
    for (auto& w : words2) {
      ++cnt2[w];
    }
    int ans = 0;
    for (auto& [w, v] : cnt1) {
      ans += v == 1 && cnt2[w] == 1;
    }
    return ans;
  }
};
func countWords(words1 []string, words2 []string) (ans int) {
  cnt1 := map[string]int{}
  cnt2 := map[string]int{}
  for _, w := range words1 {
    cnt1[w]++
  }
  for _, w := range words2 {
    cnt2[w]++
  }
  for w, v := range cnt1 {
    if v == 1 && cnt2[w] == 1 {
      ans++
    }
  }
  return
}
function countWords(words1: string[], words2: string[]): number {
  const cnt1 = new Map<string, number>();
  const cnt2 = new Map<string, number>();
  for (const w of words1) {
    cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
  }
  for (const w of words2) {
    cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
  }
  let ans = 0;
  for (const [w, v] of cnt1) {
    if (v === 1 && cnt2.get(w) === 1) {
      ++ans;
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文