Skip to content

Commit ce7cc57

Browse files
committed
feat: add solutions to lc problem: No.0916
1 parent 58c2190 commit ce7cc57

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

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

+64
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
225225
}
226226
```
227227

228+
#### TypeScript
229+
230+
```ts
231+
function wordSubsets(words1: string[], words2: string[]): string[] {
232+
const cnt2 = new Map<string, number>();
233+
234+
for (const b of words2) {
235+
const cnt = new Map<string, number>();
236+
for (const c of b) {
237+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
238+
}
239+
240+
for (const [k, v] of cnt) {
241+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
242+
}
243+
}
244+
245+
return words1.filter(a => {
246+
const cnt1 = new Map<string, number>();
247+
for (const c of a) {
248+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
249+
}
250+
251+
for (const [k, v] of cnt2) {
252+
if ((cnt1.get(k) ?? 0) < v) return false;
253+
}
254+
255+
return true;
256+
});
257+
}
258+
```
259+
260+
#### JavaScript
261+
262+
```js
263+
function wordSubsets(words1, words2) {
264+
const cnt2 = new Map();
265+
266+
for (const b of words2) {
267+
const cnt = new Map();
268+
for (const c of b) {
269+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
270+
}
271+
272+
for (const [k, v] of cnt) {
273+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
274+
}
275+
}
276+
277+
return words1.filter(a => {
278+
const cnt1 = new Map();
279+
for (const c of a) {
280+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
281+
}
282+
283+
for (const [k, v] of cnt2) {
284+
if ((cnt1.get(k) ?? 0) < v) return false;
285+
}
286+
287+
return true;
288+
});
289+
}
290+
```
291+
228292
<!-- tabs:end -->
229293
230294
<!-- solution:end -->

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

+64
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
209209
}
210210
```
211211

212+
#### TypeScript
213+
214+
```ts
215+
function wordSubsets(words1: string[], words2: string[]): string[] {
216+
const cnt2 = new Map<string, number>();
217+
218+
for (const b of words2) {
219+
const cnt = new Map<string, number>();
220+
for (const c of b) {
221+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
222+
}
223+
224+
for (const [k, v] of cnt) {
225+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
226+
}
227+
}
228+
229+
return words1.filter(a => {
230+
const cnt1 = new Map<string, number>();
231+
for (const c of a) {
232+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
233+
}
234+
235+
for (const [k, v] of cnt2) {
236+
if ((cnt1.get(k) ?? 0) < v) return false;
237+
}
238+
239+
return true;
240+
});
241+
}
242+
```
243+
244+
#### JavaScript
245+
246+
```js
247+
function wordSubsets(words1, words2) {
248+
const cnt2 = new Map();
249+
250+
for (const b of words2) {
251+
const cnt = new Map();
252+
for (const c of b) {
253+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
254+
}
255+
256+
for (const [k, v] of cnt) {
257+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
258+
}
259+
}
260+
261+
return words1.filter(a => {
262+
const cnt1 = new Map();
263+
for (const c of a) {
264+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
265+
}
266+
267+
for (const [k, v] of cnt2) {
268+
if ((cnt1.get(k) ?? 0) < v) return false;
269+
}
270+
271+
return true;
272+
});
273+
}
274+
```
275+
212276
<!-- tabs:end -->
213277
214278
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function wordSubsets(words1, words2) {
2+
const cnt2 = new Map();
3+
4+
for (const b of words2) {
5+
const cnt = new Map();
6+
for (const c of b) {
7+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
8+
}
9+
10+
for (const [k, v] of cnt) {
11+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
12+
}
13+
}
14+
15+
return words1.filter(a => {
16+
const cnt1 = new Map();
17+
for (const c of a) {
18+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
19+
}
20+
21+
for (const [k, v] of cnt2) {
22+
if ((cnt1.get(k) ?? 0) < v) return false;
23+
}
24+
25+
return true;
26+
});
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function wordSubsets(words1: string[], words2: string[]): string[] {
2+
const cnt2 = new Map<string, number>();
3+
4+
for (const b of words2) {
5+
const cnt = new Map<string, number>();
6+
for (const c of b) {
7+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
8+
}
9+
10+
for (const [k, v] of cnt) {
11+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
12+
}
13+
}
14+
15+
return words1.filter(a => {
16+
const cnt1 = new Map<string, number>();
17+
for (const c of a) {
18+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
19+
}
20+
21+
for (const [k, v] of cnt2) {
22+
if ((cnt1.get(k) ?? 0) < v) return false;
23+
}
24+
25+
return true;
26+
});
27+
}

0 commit comments

Comments
 (0)