Count Common Words With One Occurrence - Complete Solution Guide
Count Common Words With One Occurrence is LeetCode problem 2085, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 words
Detailed Explanation
The problem asks to find the number of strings that appear exactly once in both input string arrays, `words1` and `words2`. The input consists of two arrays of strings. The output is a single integer representing the count of common strings that appear exactly once in each array. Constraints limit the lengths of the arrays and individual strings.
Solution Approach
The provided solutions utilize hash tables (dictionaries, maps, unordered_maps) to store the frequency of each word in `words1` and `words2`. First, they count the occurrences of each word in each array separately. Then, they iterate through the frequency map of `words1`. For each word, they check if it exists in `words2`'s frequency map and if its frequency is 1 in both maps. If both conditions are true, the counter is incremented. Finally, the counter (the number of common words appearing exactly once in each array) is returned.
Step-by-Step Algorithm
- Step 1: Create two hash tables (dictionaries/maps) to store word frequencies for `words1` and `words2`.
- Step 2: Iterate through `words1`, updating the frequency count for each word in the `words1` frequency map.
- Step 3: Iterate through `words2`, updating the frequency count for each word in the `words2` frequency map.
- Step 4: Iterate through the keys (words) of the `words1` frequency map.
- Step 5: For each word, check if its frequency is 1 in `words1`'s map and if it exists in `words2`'s map with a frequency of 1.
- Step 6: If both conditions are true, increment the common word counter.
- Step 7: Return the final count of common words.
Key Insights
- Insight 1: Using hash tables (dictionaries in Python) to efficiently count the occurrences of each word in both arrays is crucial for achieving linear time complexity.
- Insight 2: Iterating through the frequency map of one array and checking for the existence and frequency of the same word in the other array's frequency map is the core algorithmic strategy.
- Insight 3: Handling the case where a word might not exist in one or both arrays is important to avoid errors. The `getOrDefault` method in Java and similar approaches in other languages elegantly handle this.
Complexity Analysis
Time Complexity: O(n + m)
Space Complexity: O(n + m)
Topics
This problem involves: Array, Hash Table, String, Counting.
Companies
Asked at: Jane Street.