-
Notifications
You must be signed in to change notification settings - Fork 39
/
Word Subsets.cpp
28 lines (28 loc) · 1.02 KB
/
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
class Solution {
public:
bool isPartOf(unordered_map<char, int>& a, unordered_map<char, int>& b) {
for(char c = 'a'; c <= 'z'; c++)
if(a[c] > b[c])
return false;
return true;
}
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
unordered_map<char, int> totalLetterCounter;
for(string& b : B) {
unordered_map<char, int> letterCounter; // initially empty
for(char c : b)
letterCounter[c]++;
for(char c = 'a'; c <= 'z'; c++)
totalLetterCounter[c] = max(totalLetterCounter[c], letterCounter[c]);
}
vector<string> universalWords;
for(string& a : A) {
unordered_map<char, int> letterCounter; // initially empty
for(char c : a)
letterCounter[c]++;
if(isPartOf(totalLetterCounter, letterCounter))
universalWords.push_back(a);
}
return universalWords;
}
};