Watch 10 video solutions for Word Subsets, a medium level problem involving Array, Hash Table, String. This walkthrough by NeetCodeIO has 9,451 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |