diff --git a/cpp/_916.cpp b/cpp/_916.cpp new file mode 100644 index 0000000000..7ac4a644a1 --- /dev/null +++ b/cpp/_916.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector wordSubsets(vector& words1, vector& 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 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; + } +};