Skip to content

Commit e3f477d

Browse files
authored
_916.cpp (#190)
* Create _916 Solution for the leetcode daily question 916. Word Subsets * Rename _916 to _916.cpp
1 parent d421171 commit e3f477d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

cpp/_916.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
4+
int maxCharFreq[26] = {0};
5+
int tempCharFreq[26];
6+
for (const auto& word : words2) {
7+
memset(tempCharFreq, 0, sizeof tempCharFreq);
8+
for (char ch : word) {
9+
tempCharFreq[ch - 'a']++;
10+
}
11+
for (int i = 0; i < 26; ++i) {
12+
maxCharFreq[i] = max(maxCharFreq[i], tempCharFreq[i]);
13+
}
14+
}
15+
vector<string> universalWords;
16+
for (const auto& word : words1) {
17+
memset(tempCharFreq, 0, sizeof tempCharFreq);
18+
for (char ch : word) {
19+
tempCharFreq[ch - 'a']++;
20+
}
21+
bool isUniversal = true;
22+
for (int i = 0; i < 26; ++i) {
23+
if (maxCharFreq[i] > tempCharFreq[i]) {
24+
isUniversal = false;
25+
break;
26+
}
27+
}
28+
if (isUniversal) {
29+
universalWords.emplace_back(word);
30+
}
31+
}
32+
return universalWords;
33+
}
34+
};

0 commit comments

Comments
 (0)