Skip to content

Commit 1293ec2

Browse files
committed
Solution as on 31-07-2022 10:45 pm
1 parent 3dcaa63 commit 1293ec2

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

0916. Word Subsets.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// 916. Word Subsets
2+
3+
class Solution
4+
{
5+
public:
6+
bool check(unordered_map<char, int> m, unordered_map<char, int> mp, string str)
7+
{
8+
for (int i = 0; i < str.size(); ++i)
9+
{
10+
if (mp[str[i]] < m[str[i]])
11+
return false;
12+
}
13+
return true;
14+
}
15+
16+
vector<string> wordSubsets(vector<string> &words1, vector<string> &words2)
17+
{
18+
19+
vector<string> ans;
20+
21+
unordered_map<char, int> m;
22+
for (auto itr : words2)
23+
{
24+
unordered_map<char, int> m1;
25+
for (auto x : itr)
26+
{
27+
++m1[x];
28+
m[x] = max(m[x], m1[x]);
29+
}
30+
}
31+
32+
string str = "";
33+
for (auto itr : m)
34+
{
35+
for (int i = 0; i < itr.second; ++i)
36+
str += itr.first;
37+
}
38+
39+
cout << str;
40+
41+
// unordered_map<char,int> mp = generate(str);
42+
43+
for (int i = 0; i < words1.size(); ++i)
44+
{
45+
unordered_map<char, int> mp;
46+
for (auto itr : words1[i])
47+
++mp[itr];
48+
49+
if (check(m, mp, str))
50+
ans.push_back(words1[i]);
51+
}
52+
53+
return ans;
54+
}
55+
};
56+
57+
class Solution
58+
{
59+
public:
60+
vector<string> wordSubsets(vector<string> &words1, vector<string> &words2)
61+
{
62+
63+
vector<int> freq(26, 0);
64+
65+
for (auto itr : words2)
66+
{
67+
vector<int> temp(26, 0);
68+
for (auto x : itr)
69+
{
70+
++temp[x - 'a'];
71+
freq[x - 'a'] = max(temp[x - 'a'], freq[x - 'a']);
72+
}
73+
}
74+
75+
vector<string> ans;
76+
77+
for (auto itr : words1)
78+
{
79+
vector<int> temp(26, 0);
80+
for (auto x : itr)
81+
++temp[x - 'a'];
82+
83+
bool flag = true;
84+
85+
for (int i = 0; i < 26; ++i)
86+
{
87+
if (freq[i] > temp[i])
88+
{
89+
flag = false;
90+
break;
91+
}
92+
}
93+
if (flag)
94+
ans.push_back(itr);
95+
}
96+
return ans;
97+
}
98+
};

0 commit comments

Comments
 (0)