916. Word Subsets LeetCode Solution
In this guide, you will get 916. Word Subsets LeetCode Solution with the best time and space complexity. The solution to Word Subsets problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Word Subsets solution in C++
- Word Subsets solution in Java
- Word Subsets solution in Python
- Additional Resources

Problem Statement of Word Subsets
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.
For example, “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 = [“lc”,”eo”]
Output: [“leetcode”]
Example 3:
Input: words1 = [“acaac”,”cccbb”,”aacbb”,”caacc”,”bcbbb”], words2 = [“c”,”cc”,”b”]
Output: [“cccbb”]
Constraints:
1 <= words1.length, words2.length <= 104
1 <= words1[i].length, words2[i].length <= 10
words1[i] and words2[i] consist only of lowercase English letters.
All the strings of words1 are unique.
Complexity Analysis
- Time Complexity:
- Space Complexity:
916. Word Subsets LeetCode Solution in C++
class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
vector<string> ans;
vector<int> countB(26);
for (const string& b : B) {
vector<int> temp = counter(b);
for (int i = 0; i < 26; ++i)
countB[i] = max(countB[i], temp[i]);
}
for (const string& a : A)
if (isUniversal(counter(a), countB))
ans.push_back(a);
return ans;
}
private:
vector<int> counter(const string& s) {
vector<int> count(26);
for (char c : s)
++count[c - 'a'];
return count;
}
bool isUniversal(vector<int> countA, vector<int>& countB) {
for (int i = 0; i < 26; ++i)
if (countA[i] < countB[i])
return false;
return true;
}
};
/* code provided by PROGIEZ */
916. Word Subsets LeetCode Solution in Java
class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
List<String> ans = new ArrayList<>();
int[] countB = new int[26];
for (final String b : B) {
int[] temp = counter(b);
for (int i = 0; i < 26; ++i)
countB[i] = Math.max(countB[i], temp[i]);
}
for (final String a : A)
if (isUniversal(counter(a), countB))
ans.add(a);
return ans;
}
private int[] counter(final String s) {
int[] count = new int[26];
for (char c : s.toCharArray())
++count[c - 'a'];
return count;
}
private boolean isUniversal(int[] countA, int[] countB) {
for (int i = 0; i < 26; ++i)
if (countA[i] < countB[i])
return false;
return true;
}
}
// code provided by PROGIEZ
916. Word Subsets LeetCode Solution in Python
class Solution:
def wordSubsets(self, A: list[str], B: list[str]) -> list[str]:
count = collections.Counter()
for b in B:
count = count | collections.Counter(b)
return [a for a in A if collections.Counter(a) & count == count]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.