-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_916.cpp
34 lines (34 loc) · 1.09 KB
/
_916.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
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
int maxCharFreq[26] = {0};
int tempCharFreq[26];
for (const auto& word : words2) {
memset(tempCharFreq, 0, sizeof tempCharFreq);
for (char ch : word) {
tempCharFreq[ch - 'a']++;
}
for (int i = 0; i < 26; ++i) {
maxCharFreq[i] = max(maxCharFreq[i], tempCharFreq[i]);
}
}
vector<string> universalWords;
for (const auto& word : words1) {
memset(tempCharFreq, 0, sizeof tempCharFreq);
for (char ch : word) {
tempCharFreq[ch - 'a']++;
}
bool isUniversal = true;
for (int i = 0; i < 26; ++i) {
if (maxCharFreq[i] > tempCharFreq[i]) {
isUniversal = false;
break;
}
}
if (isUniversal) {
universalWords.emplace_back(word);
}
}
return universalWords;
}
};