-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path916_Word_Subsets.cpp
More file actions
50 lines (47 loc) · 1.02 KB
/
Copy path916_Word_Subsets.cpp
File metadata and controls
50 lines (47 loc) · 1.02 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
#define ll long long
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
vector<string> res;
int n = words1.size(), m = words2.size();
unordered_map<char, int> map;
for (int i = 0; i < m; ++i)
{
int m1 = words2[i].size();
unordered_map<char, int> helper;
for (int j = 0; j < m1; ++j)
helper[words2[i][j]]++;
for (auto &it : helper)
{
if (map.find(it.first) != map.end())
{
map[it.first] = max(it.second, map[it.first]);
}
else if (map.find(it.first) == map.end())
map[it.first] = it.second;
}
}
for (int i = 0; i < n; ++i)
{
unordered_map<char, int> mp = map;
int n1 = words1[i].size();
for (int j = 0; j < n1; ++j)
{
if (mp.find(words1[i][j]) != mp.end())
{
mp[words1[i][j]]--;
if (mp[words1[i][j]] == 0)
mp.erase(words1[i][j]);
}
}
if (mp.size() == 0)
res.push_back(words1[i]);
}
return res;
}
};
int main() {
return 0;
}