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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for counting common words with one occurrence is quite straightforward. We essentially check every single word in the first list against every single word in the second list to see if they match. We then count how many times each matching word appears across both lists to determine if it occurred only once.
Here's how the algorithm would work step-by-step:
def count_common_words(first_word_list, second_word_list):
common_words = []
# Iterate through the first word list.
for first_word in first_word_list:
for second_word in second_word_list:
if first_word == second_word:
common_words.append(first_word)
count_of_common_words = 0
# Check each common word's total occurences.
for word in common_words:
total_occurrences = 0
for first_word in first_word_list:
if first_word == word:
total_occurrences += 1
for second_word in second_word_list:
if second_word == word:
total_occurrences += 1
# Ensure word appears only once in each list
first_list_occurrence = 0
for first_word in first_word_list:
if first_word == word:
first_list_occurrence += 1
second_list_occurrence = 0
for second_word in second_word_list:
if second_word == word:
second_list_occurrence += 1
if first_list_occurrence == 1 and second_list_occurrence == 1:
count_of_common_words += 1
return count_of_common_words
To efficiently count common words appearing exactly once in two lists, we use a counting method. We first count the occurrences of each word in both lists separately. Then, we compare the counts to find words that appear only once in each list and are common to both.
Here's how the algorithm would work step-by-step:
def count_common_words(words1, words2):
word_counts1 = {}
word_counts2 = {}
for word in words1:
word_counts1[word] = word_counts1.get(word, 0) + 1
for word in words2:
word_counts2[word] = word_counts2.get(word, 0) + 1
common_word_count = 0
# Only check words present in both lists
for word, count in word_counts1.items():
if word in word_counts2:
# Verify that the word appears only once in each list.
if word_counts1[word] == 1 and word_counts2[word] == 1:
common_word_count += 1
return common_word_count
Case | How to Handle |
---|---|
Both input arrays are null or empty | Return 0, as there are no words to compare. |
One array is null/empty, the other is not | Return 0, as there cannot be any common words. |
Arrays contain only one word each, which are the same | Return 1, as this single word appears exactly once in both. |
Arrays contain only one word each, which are different | Return 0, as there are no common words. |
Arrays contain the same word multiple times (e.g., ['a','a','a'], ['a','a']) | The counting mechanism (e.g., using HashMaps) should ensure only words appearing exactly once in both arrays are counted. |
Arrays contain very large number of words (scalability) | Using HashMaps provides an average time complexity of O(n+m) for counting words, where n and m are the lengths of the arrays. |
Arrays contain words with different casing (e.g., 'hello' and 'Hello') | Convert all words to lowercase before processing to ensure case-insensitive comparison. |
Arrays contain words with leading or trailing spaces | Trim leading/trailing whitespace from words before processing to avoid incorrect matching. |