2085. Count Common Words With One Occurrence LeetCode Solution
In this guide, you will get 2085. Count Common Words With One Occurrence LeetCode Solution with the best time and space complexity. The solution to Count Common Words With One Occurrence problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Common Words With One Occurrence solution in C++
- Count Common Words With One Occurrence solution in Java
- Count Common Words With One Occurrence solution in Python
- Additional Resources
Problem Statement of 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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2085. Count Common Words With One Occurrence LeetCode Solution in C++
class Solution {
public:
int countWords(vector<string>& words1, vector<string>& words2) {
unordered_map<string, int> count;
for (const string& word : words1)
++count[word];
for (const string& word : words2)
if (const auto it = count.find(word);
it != count.cend() && it->second < 2)
--it->second;
return ranges::count_if(
count, [](const pair<string, int>& c) { return c.second == 0; });
}
};
/* code provided by PROGIEZ */
2085. Count Common Words With One Occurrence LeetCode Solution in Java
class Solution {
public int countWords(String[] words1, String[] words2) {
Map<String, Integer> count = new HashMap<>();
for (final String word : words1)
count.merge(word, 1, Integer::sum);
for (final String word : words2)
if (count.containsKey(word) && count.get(word) < 2)
count.merge(word, -1, Integer::sum);
return (int) count.values().stream().filter(v -> v == 0).count();
}
}
// code provided by PROGIEZ
2085. Count Common Words With One Occurrence LeetCode Solution in Python
class Solution:
def countWords(self, words1: list[str], words2: list[str]) -> int:
count = collections.Counter(words1)
for word in words2:
if word in count and count[word] < 2:
count[word] -= 1
return sum(value == 0 for value in count.values())
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.