We are given two arrays of words: words1
and words2
. Our goal is to find all words in words1
that are "universal" to all the words in words2
. A word in words1
is considered universal if it contains all the letters and their frequencies of every word in words2
.
The challenge is to efficiently check if words in words1
contain the cumulative character requirements of all words in words2
. Here's how we break it down:
- Combine Requirements from
words2
: Merge the character requirements of all words inwords2
into a single frequency map. For example, ifwords2 = ["abc", "ab"]
, the combined requirement is{a: 1, b: 1, c: 1}
. - Validate Words in
words1
: For each word inwords1
, check if it satisfies the combined requirements.
The solution involves two main steps:
- Iterate through all words in
words2
. - For each word, calculate its character frequencies.
- Maintain a global frequency map where the frequency of each character is the maximum frequency across all words in
words2
.
- For each word in
words1
, calculate its character frequencies. - Check if the word meets or exceeds the global requirements from
words2
. - If the word meets the requirements, add it to the result list.
-
Time Complexity:
- Building the global requirement:
$$O(n_2 \cdot m_2)$$ , where (n_2) is the number of words inwords2
, and (m_2) is the average length of words inwords2
. - Validating words:
$$O(n_1 \cdot m_1)$$ , where (n_1) is the number of words inwords1
, and (m_1) is the average length of words inwords1
. - Overall:
$$O(n_1 \cdot m_1 + n_2 \cdot m_2)$$ .
- Building the global requirement:
-
Space Complexity:
$$O(26) = O(1)$$ for character frequency arrays.
- Global Frequency Calculation:
- Use a vector of size 26 to store the maximum frequency of each character in
words2
. - Iterate through
words2
, updating the global frequency map as needed.
- Use a vector of size 26 to store the maximum frequency of each character in
- Validation of Words:
- For each word in
words1
, calculate its character frequencies. - Compare the frequencies with the global requirement from
words2
.
- For each word in
- Final Result:
- If a word satisfies all character requirements, include it in the result list.
- Global Frequency Calculation:
- Use an integer array of size 26 for the maximum frequency of each character in
words2
. - Traverse through each word in
words2
, updating the array to reflect the highest frequency of each character.
- Use an integer array of size 26 for the maximum frequency of each character in
- Validation of Words:
- For every word in
words1
, compute its character frequencies. - Match these frequencies against the global frequency array.
- For every word in
- Final Result:
- If all conditions are met, add the word to the output list.
- Global Frequency Calculation:
- Use an array of size 26 to track the maximum frequency of characters in
words2
. - Loop through
words2
, calculating frequencies and updating the global array as necessary.
- Use an array of size 26 to track the maximum frequency of characters in
- Validation of Words:
- For each word in
words1
, calculate its character counts. - Compare these counts with the global requirements from
words2
.
- For each word in
- Final Result:
- Add words that meet the requirements to the result array.
- Global Frequency Calculation:
- Utilize a list of size 26 to store the maximum frequency for each character.
- Iterate over
words2
, computing character frequencies for each word and updating the global list.
- Validation of Words:
- For every word in
words1
, compute its character frequencies using a helper function. - Compare the frequencies against the global frequency list.
- For every word in
- Final Result:
- Add valid words to the output list.
- Global Frequency Calculation:
- Use a fixed-size array of integers (size 26) for the maximum character frequencies from
words2
. - Process each word in
words2
, updating the array to capture the maximum frequencies.
- Use a fixed-size array of integers (size 26) for the maximum character frequencies from
- Validation of Words:
- For each word in
words1
, compute character frequencies using a helper function. - Validate these frequencies against the global requirement array.
- For each word in
- Final Result:
- Append words that satisfy the global character requirements to the result slice.
words1 = ["amazon", "apple", "facebook", "google", "leetcode"]
words2 = ["e", "o"]
["facebook", "google", "leetcode"]
- Each language implementation uses character frequency arrays to optimize the solution, ensuring efficient comparison of character counts.
- This approach minimizes unnecessary recomputation and ensures a clean, modular design across all implementations.
Feel free to adapt the solution to suit your specific needs or optimize further based on constraints! 🎉