952. Word Subsets
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find all strings in words1 that are "universal". A string a is universal if every string b from words2 is a subset of a. A string b is a subset of a if all characters in b, including their counts (multiplicity), are present in a. For example, "aabc" is a subset of "abacda" because "abacda" contains at least two 'a's, one 'b', and one 'c'.
Solution
Word Subsets in Python
class Solution:
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
# Helper function to count character frequencies in a word.
def count_freq(word: str) -> List[int]:
freq = [0] * 26
for char in word:
freq[ord(char) - ord('a')] += 1
return freq
# Step 1: Determine the maximum frequency requirements from all words in words2.
# This creates a single frequency map representing the superset of all words in words2.
max_freq_b = [0] * 26
for b in words2:
freq_b = count_freq(b)
for i in range(26):
max_freq_b[i] = max(max_freq_b[i], freq_b[i])
result = []
# Step 2: Check each word in words1 to see if it's a universal string.
for a in words1:
freq_a = count_freq(a)
is_universal = True
for i in range(26):
# If word 'a' has fewer characters than required, it's not universal.
if freq_a[i] < max_freq_b[i]:
is_universal = False
break
if is_universal:
result.append(a)
return result
Word Subsets in Java
class Solution {
public List<String> wordSubsets(String[] words1, String[] words2) {
// Create a frequency map to store the maximum frequency of each character
// required by any word in words2.
int[] maxFreqB = new int[26];
// Step 1: Determine the maximum frequency requirements from all words in words2.
for (String b : words2) {
int[] freqB = count(b);
for (int i = 0; i < 26; i++) {
maxFreqB[i] = Math.max(maxFreqB[i], freqB[i]);
}
}
List<String> result = new ArrayList<>();
// Step 2: Check each word in words1 to see if it's a universal string.
for (String a : words1) {
int[] freqA = count(a);
boolean isUniversal = true;
for (int i = 0; i < 26; i++) {
// If word 'a' has fewer characters than required, it's not a superset.
if (freqA[i] < maxFreqB[i]) {
isUniversal = false;
break;
}
}
if (isUniversal) {
result.add(a);
}
}
return result;
}
// Helper function to count character frequencies in a string.
private int[] count(String s) {
int[] freq = new int[26];
for (char c : s.toCharArray()) {
freq[c - 'a']++;
}
return freq;
}
}
Word Subsets in C++
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
// Create a frequency map to store the maximum frequency of each character
// required by any word in words2.
vector<int> maxFreqB(26, 0);
// Helper function to count character frequencies in a string.
auto countFreq = [](const string& s) {
vector<int> freq(26, 0);
for (char c : s) {
freq[c - 'a']++;
}
return freq;
};
// Step 1: Determine the maximum frequency requirements from all words in words2.
for (const string& b : words2) {
vector<int> freqB = countFreq(b);
for (int i = 0; i < 26; ++i) {
maxFreqB[i] = max(maxFreqB[i], freqB[i]);
}
}
vector<string> result;
// Step 2: Check each word in words1 to see if it's a universal string.
for (const string& a : words1) {
vector<int> freqA = countFreq(a);
bool isUniversal = true;
for (int i = 0; i < 26; ++i) {
// If word 'a' has fewer characters than required, it's not a superset.
if (freqA[i] < maxFreqB[i]) {
isUniversal = false;
break;
}
}
if (isUniversal) {
result.push_back(a);
}
}
return result;
}
};
Word Subsets in TypeScript
function wordSubsets(words1: string[], words2: string[]): string[] {
// Helper function to count character frequencies in a word.
const countFreq = (word: string): number[] => {
const freq = new Array(26).fill(0);
for (const char of word) {
freq[char.charCodeAt(0) - 'a'.charCodeAt(0)]++;
}
return freq;
};
// Step 1: Determine the maximum frequency requirements from all words in words2.
// This creates a single frequency map representing the superset of all words in words2.
const maxFreqB = new Array(26).fill(0);
for (const b of words2) {
const freqB = countFreq(b);
for (let i = 0; i < 26; i++) {
maxFreqB[i] = Math.max(maxFreqB[i], freqB[i]);
}
}
const result: string[] = [];
// Step 2: Check each word in words1 to see if it's a universal string.
for (const a of words1) {
const freqA = countFreq(a);
let isUniversal = true;
for (let i = 0; i < 26; i++) {
// If word 'a' has fewer characters than required, it's not universal.
if (freqA[i] < maxFreqB[i]) {
isUniversal = false;
break;
}
}
if (isUniversal) {
result.push(a);
}
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.