Skip to content

Commit 2963098

Browse files
committed
feat: add solutions to lc problem: No.0916
1 parent e27eeca commit 2963098

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

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

Lines changed: 64 additions & 0 deletions
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 hash2 = new Map<string, number>();
233+
234+
for (const w of words2) {
235+
const hash = new Map<string, number>();
236+
for (const ch of w) {
237+
hash.set(ch, (hash.get(ch) ?? 0) + 1);
238+
}
239+
240+
for (const [k, v] of hash) {
241+
hash2.set(k, Math.max(hash2.get(k) ?? 0, v));
242+
}
243+
}
244+
245+
return words1.filter(w => {
246+
const hash1 = new Map<string, number>();
247+
for (const ch of w) {
248+
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
249+
}
250+
251+
for (const [k, v] of hash2) {
252+
if ((hash1.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 hash2 = new Map();
265+
266+
for (const w of words2) {
267+
const hash = new Map();
268+
for (const ch of w) {
269+
hash.set(ch, (hash.get(ch) ?? 0) + 1);
270+
}
271+
272+
for (const [k, v] of hash) {
273+
hash2.set(k, Math.max(hash2.get(k) ?? 0, v));
274+
}
275+
}
276+
277+
return words1.filter(w => {
278+
const hash1 = new Map();
279+
for (const ch of w) {
280+
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
281+
}
282+
283+
for (const [k, v] of hash2) {
284+
if ((hash1.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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
199199
}
200200
```
201201

202+
#### TypeScript
203+
204+
```ts
205+
function wordSubsets(words1: string[], words2: string[]): string[] {
206+
const hash2 = new Map<string, number>();
207+
208+
for (const w of words2) {
209+
const hash = new Map<string, number>();
210+
for (const ch of w) {
211+
hash.set(ch, (hash.get(ch) ?? 0) + 1);
212+
}
213+
214+
for (const [k, v] of hash) {
215+
hash2.set(k, Math.max(hash2.get(k) ?? 0, v));
216+
}
217+
}
218+
219+
return words1.filter(w => {
220+
const hash1 = new Map<string, number>();
221+
for (const ch of w) {
222+
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
223+
}
224+
225+
for (const [k, v] of hash2) {
226+
if ((hash1.get(k) ?? 0) < v) return false;
227+
}
228+
229+
return true;
230+
});
231+
}
232+
```
233+
234+
#### JavaScript
235+
236+
```js
237+
function wordSubsets(words1, words2) {
238+
const hash2 = new Map();
239+
240+
for (const w of words2) {
241+
const hash = new Map();
242+
for (const ch of w) {
243+
hash.set(ch, (hash.get(ch) ?? 0) + 1);
244+
}
245+
246+
for (const [k, v] of hash) {
247+
hash2.set(k, Math.max(hash2.get(k) ?? 0, v));
248+
}
249+
}
250+
251+
return words1.filter(w => {
252+
const hash1 = new Map();
253+
for (const ch of w) {
254+
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
255+
}
256+
257+
for (const [k, v] of hash2) {
258+
if ((hash1.get(k) ?? 0) < v) return false;
259+
}
260+
261+
return true;
262+
});
263+
}
264+
```
265+
202266
<!-- tabs:end -->
203267
204268
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function wordSubsets(words1, words2) {
2+
const hash2 = new Map();
3+
4+
for (const w of words2) {
5+
const hash = new Map();
6+
for (const ch of w) {
7+
hash.set(ch, (hash.get(ch) ?? 0) + 1);
8+
}
9+
10+
for (const [k, v] of hash) {
11+
hash2.set(k, Math.max(hash2.get(k) ?? 0, v));
12+
}
13+
}
14+
15+
return words1.filter(w => {
16+
const hash1 = new Map();
17+
for (const ch of w) {
18+
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
19+
}
20+
21+
for (const [k, v] of hash2) {
22+
if ((hash1.get(k) ?? 0) < v) return false;
23+
}
24+
25+
return true;
26+
});
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function wordSubsets(words1: string[], words2: string[]): string[] {
2+
const hash2 = new Map<string, number>();
3+
4+
for (const w of words2) {
5+
const hash = new Map<string, number>();
6+
for (const ch of w) {
7+
hash.set(ch, (hash.get(ch) ?? 0) + 1);
8+
}
9+
10+
for (const [k, v] of hash) {
11+
hash2.set(k, Math.max(hash2.get(k) ?? 0, v));
12+
}
13+
}
14+
15+
return words1.filter(w => {
16+
const hash1 = new Map<string, number>();
17+
for (const ch of w) {
18+
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
19+
}
20+
21+
for (const [k, v] of hash2) {
22+
if ((hash1.get(k) ?? 0) < v) return false;
23+
}
24+
25+
return true;
26+
});
27+
}

0 commit comments

Comments
 (0)