Skip to content

Commit 073e223

Browse files
committed
feat: add solutions to lc problem: No.0916
No.0916.Word Subsets
1 parent 05ad303 commit 073e223

File tree

6 files changed

+371
-2
lines changed

6 files changed

+371
-2
lines changed

solution/0900-0999/0916.Word Subsets/README.md

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,153 @@
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

76+
**方法一:计数**
77+
78+
遍历 `words2` 中的每个单词 `b`,统计每个字母出现的最大次数,记为 `cnt`
79+
80+
然后遍历 `words1` 中的每个单词 `a`,统计每个字母出现的次数,记为 `t`。如果 `cnt` 中的每个字母的出现次数都不大于 `t` 中的出现次数,则 `a` 是通用单词,将其加入答案。
81+
82+
时间复杂度 $O(L)$,其中 $L$ 为 `words1``words2` 中所有单词的长度之和。
83+
7684
<!-- tabs:start -->
7785

7886
### **Python3**
7987

8088
<!-- 这里可写当前语言的特殊实现逻辑 -->
8189

8290
```python
83-
91+
class Solution:
92+
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
93+
cnt = Counter()
94+
for b in words2:
95+
t = Counter(b)
96+
for c, v in t.items():
97+
cnt[c] = max(cnt[c], v)
98+
ans = []
99+
for a in words1:
100+
t = Counter(a)
101+
if all(v <= t[c] for c, v in cnt.items()):
102+
ans.append(a)
103+
return ans
84104
```
85105

86106
### **Java**
87107

88108
<!-- 这里可写当前语言的特殊实现逻辑 -->
89109

90110
```java
111+
class Solution {
112+
public List<String> wordSubsets(String[] words1, String[] words2) {
113+
int[] cnt = new int[26];
114+
for (var b : words2) {
115+
int[] t = new int[26];
116+
for (int i = 0; i < b.length(); ++i) {
117+
t[b.charAt(i) - 'a']++;
118+
}
119+
for (int i = 0; i < 26; ++i) {
120+
cnt[i] = Math.max(cnt[i], t[i]);
121+
}
122+
}
123+
List<String> ans = new ArrayList<>();
124+
for (var a : words1) {
125+
int[] t = new int[26];
126+
for (int i = 0; i < a.length(); ++i) {
127+
t[a.charAt(i) - 'a']++;
128+
}
129+
boolean ok = true;
130+
for (int i = 0; i < 26; ++i) {
131+
if (cnt[i] > t[i]) {
132+
ok = false;
133+
break;
134+
}
135+
}
136+
if (ok) {
137+
ans.add(a);
138+
}
139+
}
140+
return ans;
141+
}
142+
}
143+
```
144+
145+
### **C++**
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
151+
int cnt[26] = {0};
152+
int t[26];
153+
for (auto& b : words2) {
154+
memset(t, 0, sizeof t);
155+
for (auto& c : b) {
156+
t[c - 'a']++;
157+
}
158+
for (int i = 0; i < 26; ++i) {
159+
cnt[i] = max(cnt[i], t[i]);
160+
}
161+
}
162+
vector<string> ans;
163+
for (auto& a : words1) {
164+
memset(t, 0, sizeof t);
165+
for (auto& c : a) {
166+
t[c - 'a']++;
167+
}
168+
bool ok = true;
169+
for (int i = 0; i < 26; ++i) {
170+
if (cnt[i] > t[i]) {
171+
ok = false;
172+
break;
173+
}
174+
}
175+
if (ok) {
176+
ans.emplace_back(a);
177+
}
178+
}
179+
return ans;
180+
}
181+
};
182+
```
91183
184+
### **Go**
185+
186+
```go
187+
func wordSubsets(words1 []string, words2 []string) (ans []string) {
188+
cnt := [26]int{}
189+
for _, b := range words2 {
190+
t := [26]int{}
191+
for _, c := range b {
192+
t[c-'a']++
193+
}
194+
for i := range cnt {
195+
cnt[i] = max(cnt[i], t[i])
196+
}
197+
}
198+
for _, a := range words1 {
199+
t := [26]int{}
200+
for _, c := range a {
201+
t[c-'a']++
202+
}
203+
ok := true
204+
for i, v := range cnt {
205+
if v > t[i] {
206+
ok = false
207+
break
208+
}
209+
}
210+
if ok {
211+
ans = append(ans, a)
212+
}
213+
}
214+
return
215+
}
216+
217+
func max(a, b int) int {
218+
if a > b {
219+
return a
220+
}
221+
return b
222+
}
92223
```
93224

94225
### **...**

solution/0900-0999/0916.Word Subsets/README_EN.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,136 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
53+
cnt = Counter()
54+
for b in words2:
55+
t = Counter(b)
56+
for c, v in t.items():
57+
cnt[c] = max(cnt[c], v)
58+
ans = []
59+
for a in words1:
60+
t = Counter(a)
61+
if all(v <= t[c] for c, v in cnt.items()):
62+
ans.append(a)
63+
return ans
5264
```
5365

5466
### **Java**
5567

5668
```java
69+
class Solution {
70+
public List<String> wordSubsets(String[] words1, String[] words2) {
71+
int[] cnt = new int[26];
72+
for (var b : words2) {
73+
int[] t = new int[26];
74+
for (int i = 0; i < b.length(); ++i) {
75+
t[b.charAt(i) - 'a']++;
76+
}
77+
for (int i = 0; i < 26; ++i) {
78+
cnt[i] = Math.max(cnt[i], t[i]);
79+
}
80+
}
81+
List<String> ans = new ArrayList<>();
82+
for (var a : words1) {
83+
int[] t = new int[26];
84+
for (int i = 0; i < a.length(); ++i) {
85+
t[a.charAt(i) - 'a']++;
86+
}
87+
boolean ok = true;
88+
for (int i = 0; i < 26; ++i) {
89+
if (cnt[i] > t[i]) {
90+
ok = false;
91+
break;
92+
}
93+
}
94+
if (ok) {
95+
ans.add(a);
96+
}
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
109+
int cnt[26] = {0};
110+
int t[26];
111+
for (auto& b : words2) {
112+
memset(t, 0, sizeof t);
113+
for (auto& c : b) {
114+
t[c - 'a']++;
115+
}
116+
for (int i = 0; i < 26; ++i) {
117+
cnt[i] = max(cnt[i], t[i]);
118+
}
119+
}
120+
vector<string> ans;
121+
for (auto& a : words1) {
122+
memset(t, 0, sizeof t);
123+
for (auto& c : a) {
124+
t[c - 'a']++;
125+
}
126+
bool ok = true;
127+
for (int i = 0; i < 26; ++i) {
128+
if (cnt[i] > t[i]) {
129+
ok = false;
130+
break;
131+
}
132+
}
133+
if (ok) {
134+
ans.emplace_back(a);
135+
}
136+
}
137+
return ans;
138+
}
139+
};
140+
```
57141
142+
### **Go**
143+
144+
```go
145+
func wordSubsets(words1 []string, words2 []string) (ans []string) {
146+
cnt := [26]int{}
147+
for _, b := range words2 {
148+
t := [26]int{}
149+
for _, c := range b {
150+
t[c-'a']++
151+
}
152+
for i := range cnt {
153+
cnt[i] = max(cnt[i], t[i])
154+
}
155+
}
156+
for _, a := range words1 {
157+
t := [26]int{}
158+
for _, c := range a {
159+
t[c-'a']++
160+
}
161+
ok := true
162+
for i, v := range cnt {
163+
if v > t[i] {
164+
ok = false
165+
break
166+
}
167+
}
168+
if ok {
169+
ans = append(ans, a)
170+
}
171+
}
172+
return
173+
}
174+
175+
func max(a, b int) int {
176+
if a > b {
177+
return a
178+
}
179+
return b
180+
}
58181
```
59182

60183
### **...**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
4+
int cnt[26] = {0};
5+
int t[26];
6+
for (auto& b : words2) {
7+
memset(t, 0, sizeof t);
8+
for (auto& c : b) {
9+
t[c - 'a']++;
10+
}
11+
for (int i = 0; i < 26; ++i) {
12+
cnt[i] = max(cnt[i], t[i]);
13+
}
14+
}
15+
vector<string> ans;
16+
for (auto& a : words1) {
17+
memset(t, 0, sizeof t);
18+
for (auto& c : a) {
19+
t[c - 'a']++;
20+
}
21+
bool ok = true;
22+
for (int i = 0; i < 26; ++i) {
23+
if (cnt[i] > t[i]) {
24+
ok = false;
25+
break;
26+
}
27+
}
28+
if (ok) {
29+
ans.emplace_back(a);
30+
}
31+
}
32+
return ans;
33+
}
34+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
func wordSubsets(words1 []string, words2 []string) (ans []string) {
2+
cnt := [26]int{}
3+
for _, b := range words2 {
4+
t := [26]int{}
5+
for _, c := range b {
6+
t[c-'a']++
7+
}
8+
for i := range cnt {
9+
cnt[i] = max(cnt[i], t[i])
10+
}
11+
}
12+
for _, a := range words1 {
13+
t := [26]int{}
14+
for _, c := range a {
15+
t[c-'a']++
16+
}
17+
ok := true
18+
for i, v := range cnt {
19+
if v > t[i] {
20+
ok = false
21+
break
22+
}
23+
}
24+
if ok {
25+
ans = append(ans, a)
26+
}
27+
}
28+
return
29+
}
30+
31+
func max(a, b int) int {
32+
if a > b {
33+
return a
34+
}
35+
return b
36+
}

0 commit comments

Comments
 (0)