You are given two string arrays words1 and words2.
A string b is a subset of string a if every letter in b occurs in a including multiplicity.
"wrr" is a subset of "warrior" but is not a subset of "world".A string a from words1 is universal if for every string b in words2, b is a subset of a.
Return an array of all the universal strings in words1. You may return the answer in any order.
Example 1:
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"] Output: ["facebook","google","leetcode"]
Example 2:
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"] Output: ["apple","google","leetcode"]
Constraints:
1 <= words1.length, words2.length <= 1041 <= words1[i].length, words2[i].length <= 10words1[i] and words2[i] consist only of lowercase English letters.words1 are unique.Problem Overview: You are given two string arrays words1 and words2. A word from words1 is considered universal if it contains every character required by each word in words2, including the correct frequency. The task is to return all such universal words.
Approach 1: Character Set Checking (O(n * m * k) time, O(1) space)
The direct approach compares every word in words1 with every word in words2. For each pair, build a frequency count of characters and verify whether the word from words1 contains at least the same frequency of each character required by the word in words2. If the check fails for any word in words2, the candidate word is rejected. This approach uses simple character frequency arrays (size 26 for lowercase letters) and repeated comparisons.
The drawback is redundant work. If words2 contains overlapping requirements like "e" and "ee", the same checks are repeated many times. Time complexity becomes O(n * m * k), where n is the size of words1, m is the size of words2, and k is the average word length. Space complexity stays O(1) because the alphabet size is fixed. This method relies on basic operations over string processing and character counting.
Approach 2: Frequency Map Aggregation (O(n * k + m * k) time, O(1) space)
The key insight is that every universal word must satisfy the maximum frequency requirement for each character across all words in words2. Instead of checking each requirement individually, compute a single aggregated frequency map. For every character c, store the maximum count required among all words in words2. For example, if words2 contains "e" and "ee", the requirement for e becomes 2.
Once this aggregated requirement is built, iterate through words1. For each word, compute its frequency array and verify that it meets or exceeds the required counts for all characters. If it does, add the word to the result list. This eliminates repeated checks against each word in words2.
The preprocessing step over words2 costs O(m * k). Checking all words in words1 costs O(n * k). Since the alphabet is fixed, the space complexity remains O(1). This solution heavily uses counting techniques common in hash table problems and array-based frequency tracking.
Recommended for interviews: The frequency aggregation approach is the expected solution. It shows that you can recognize repeated constraints and compress them into a single requirement. Interviewers often accept the brute-force comparison first to demonstrate understanding, but the optimized aggregation approach demonstrates stronger algorithmic reasoning and reduces unnecessary checks.
This approach involves using a frequency map to determine the maximum frequency of each letter required by words2 that must be present in each word in words1 to be considered universal. We then check each word in words1 to see if it satisfies these maximum frequency requirements.
This solution first calculates the maximum frequency for each letter that appears in words2 using a counter. Then, it iterates over words1 to verify if each word contains at least the maximum frequencies for all letters calculated. If so, it adds the word to the result list.
Time Complexity: O(N * S + M * T), where N is the length of words1, M is the length of words2, S is the average length of a word in words1, and T is the average length of a word in words2. The space complexity is O(1), as we are using a fixed character set.
In this approach, we create a character frequency check vector for words2 and verify each word in words1 against this vector. Instead of a hashmap, we'll use arrays as fixed length makes space and access efficient.
This Java solution uses an integer array of size 26 to store character frequencies, making it efficient in terms of both space and time for access. We compute the aggregate maximum frequency needed, and check words in words1 against this frequency array directly.
Java
JavaScript
Time Complexity: O(N * S + M * T), Space Complexity: O(1).
Traverse each word b in words2, count the maximum occurrence of each letter, and record it as cnt.
Then traverse each word a in words1, count the occurrence of each letter, and record it as t. If the occurrence of each letter in cnt is not greater than the occurrence in t, then a is a universal word, and add it to the answer.
The time complexity is O(L), where L is the sum of the lengths of all words in words1 and words2.
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Frequency Map Aggregation | Time Complexity: O(N * S + M * T), where N is the length of words1, M is the length of words2, S is the average length of a word in words1, and T is the average length of a word in words2. The space complexity is O(1), as we are using a fixed character set. |
| Character Set Checking | Time Complexity: O(N * S + M * T), Space Complexity: O(1). |
| Counting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Character Set Checking | O(n * m * k) | O(1) | Simple baseline approach or when constraints are very small |
| Frequency Map Aggregation | O(n * k + m * k) | O(1) | Optimal approach for interviews and large inputs |
Word Subsets - Leetcode 916 - Python • NeetCodeIO • 9,451 views views
Watch 9 more video solutions →Practice Word Subsets with our built-in code editor and test cases.
Practice on FleetCode