Skip to content

Commit 3382362

Browse files
committed
feat: add solutions to lc problem: No.0916
1 parent 2bbd0ff commit 3382362

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
@@ -214,6 +214,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
214214
}
215215
```
216216

217+
#### TypeScript
218+
219+
```ts
220+
function wordSubsets(words1: string[], words2: string[]): string[] {
221+
const cnt2 = new Map<string, number>();
222+
223+
for (const b of words2) {
224+
const cnt = new Map<string, number>();
225+
for (const c of b) {
226+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
227+
}
228+
229+
for (const [k, v] of cnt) {
230+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
231+
}
232+
}
233+
234+
return words1.filter(a => {
235+
const cnt1 = new Map<string, number>();
236+
for (const c of a) {
237+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
238+
}
239+
240+
for (const [k, v] of cnt2) {
241+
if ((cnt1.get(k) ?? 0) < v) return false;
242+
}
243+
244+
return true;
245+
});
246+
}
247+
```
248+
249+
#### JavaScript
250+
251+
```js
252+
function wordSubsets(words1, words2) {
253+
const cnt2 = new Map();
254+
255+
for (const b of words2) {
256+
const cnt = new Map();
257+
for (const c of b) {
258+
cnt.set(c, (cnt.get(c) ?? 0) + 1);
259+
}
260+
261+
for (const [k, v] of cnt) {
262+
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
263+
}
264+
}
265+
266+
return words1.filter(a => {
267+
const cnt1 = new Map();
268+
for (const c of a) {
269+
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
270+
}
271+
272+
for (const [k, v] of cnt2) {
273+
if ((cnt1.get(k) ?? 0) < v) return false;
274+
}
275+
276+
return true;
277+
});
278+
}
279+
```
280+
217281
<!-- tabs:end -->
218282
219283
<!-- 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)