-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_Word_Subsets.cpp
86 lines (64 loc) · 2.65 KB
/
10_Word_Subsets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 916. 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 = ["l","e"]
// Output: ["apple","google","leetcode"]
// 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.
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
vector<int> freq(26, 0);
for(auto& w : words2) {
vector<int> temp(26, 0);
for(char c : w) {
temp[c - 'a']++;
}
for(int i = 0; i < 26; i++) {
freq[i] = max(freq[i], temp[i]);
}
}
vector<string> res;
for(auto& w : words1) {
vector<int> temp(26, 0);
for(char c : w) {
temp[c - 'a']++;
}
int i;
for(i = 0; i < 26; i++) {
if(temp[i] < freq[i]) {
break;
}
}
if(i == 26) {
res.push_back(w);
}
}
return res;
}
};
/*
This code solves the Word Subsets problem. Here's how it works:
1. First, it creates a frequency array 'freq' to store the maximum frequency of each character required from words2.
2. For each word in words2:
- Creates a temporary frequency array for current word
- Counts frequency of each character in current word
- Updates the main frequency array with maximum values
3. Then for each word in words1:
- Creates a frequency array for current word
- Checks if this word has enough of each character to satisfy the requirements
- If all requirements are met (loop reaches 26), adds word to result
4. Finally returns the array of universal strings
The solution uses character frequency counting and comparison to efficiently determine which strings from words1 are universal.
Time complexity is O(N*K) where N is total characters in all words and K is alphabet size (26).
*/