File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments