Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0916
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 committed Jan 16, 2025
1 parent 58c2190 commit ce7cc57
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
64 changes: 64 additions & 0 deletions solution/0900-0999/0916.Word Subsets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
}
```

#### TypeScript

```ts
function wordSubsets(words1: string[], words2: string[]): string[] {
const cnt2 = new Map<string, number>();

for (const b of words2) {
const cnt = new Map<string, number>();
for (const c of b) {
cnt.set(c, (cnt.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt) {
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
}
}

return words1.filter(a => {
const cnt1 = new Map<string, number>();
for (const c of a) {
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt2) {
if ((cnt1.get(k) ?? 0) < v) return false;
}

return true;
});
}
```

#### JavaScript

```js
function wordSubsets(words1, words2) {
const cnt2 = new Map();

for (const b of words2) {
const cnt = new Map();
for (const c of b) {
cnt.set(c, (cnt.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt) {
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
}
}

return words1.filter(a => {
const cnt1 = new Map();
for (const c of a) {
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt2) {
if ((cnt1.get(k) ?? 0) < v) return false;
}

return true;
});
}
```
<!-- tabs:end -->
<!-- solution:end -->
Expand Down
64 changes: 64 additions & 0 deletions solution/0900-0999/0916.Word Subsets/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
}
```

#### TypeScript

```ts
function wordSubsets(words1: string[], words2: string[]): string[] {
const cnt2 = new Map<string, number>();

for (const b of words2) {
const cnt = new Map<string, number>();
for (const c of b) {
cnt.set(c, (cnt.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt) {
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
}
}

return words1.filter(a => {
const cnt1 = new Map<string, number>();
for (const c of a) {
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt2) {
if ((cnt1.get(k) ?? 0) < v) return false;
}

return true;
});
}
```

#### JavaScript

```js
function wordSubsets(words1, words2) {
const cnt2 = new Map();

for (const b of words2) {
const cnt = new Map();
for (const c of b) {
cnt.set(c, (cnt.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt) {
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
}
}

return words1.filter(a => {
const cnt1 = new Map();
for (const c of a) {
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt2) {
if ((cnt1.get(k) ?? 0) < v) return false;
}

return true;
});
}
```
<!-- tabs:end -->
<!-- solution:end -->
Expand Down
27 changes: 27 additions & 0 deletions solution/0900-0999/0916.Word Subsets/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function wordSubsets(words1, words2) {
const cnt2 = new Map();

for (const b of words2) {
const cnt = new Map();
for (const c of b) {
cnt.set(c, (cnt.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt) {
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
}
}

return words1.filter(a => {
const cnt1 = new Map();
for (const c of a) {
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt2) {
if ((cnt1.get(k) ?? 0) < v) return false;
}

return true;
});
}
27 changes: 27 additions & 0 deletions solution/0900-0999/0916.Word Subsets/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function wordSubsets(words1: string[], words2: string[]): string[] {
const cnt2 = new Map<string, number>();

for (const b of words2) {
const cnt = new Map<string, number>();
for (const c of b) {
cnt.set(c, (cnt.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt) {
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
}
}

return words1.filter(a => {
const cnt1 = new Map<string, number>();
for (const c of a) {
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
}

for (const [k, v] of cnt2) {
if ((cnt1.get(k) ?? 0) < v) return false;
}

return true;
});
}

0 comments on commit ce7cc57

Please sign in to comment.