Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hash2 = new Map<string, number>();

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

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

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

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

return true;
});
}
```

#### JavaScript

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

for (const w of words2) {
const hash = new Map();
for (const ch of w) {
hash.set(ch, (hash.get(ch) ?? 0) + 1);
}

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

return words1.filter(w => {
const hash1 = new Map();
for (const ch of w) {
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
}

for (const [k, v] of hash2) {
if ((hash1.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 @@ -199,6 +199,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
}
```

#### TypeScript

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

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

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

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

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

return true;
});
}
```

#### JavaScript

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

for (const w of words2) {
const hash = new Map();
for (const ch of w) {
hash.set(ch, (hash.get(ch) ?? 0) + 1);
}

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

return words1.filter(w => {
const hash1 = new Map();
for (const ch of w) {
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
}

for (const [k, v] of hash2) {
if ((hash1.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 hash2 = new Map();

for (const w of words2) {
const hash = new Map();
for (const ch of w) {
hash.set(ch, (hash.get(ch) ?? 0) + 1);
}

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

return words1.filter(w => {
const hash1 = new Map();
for (const ch of w) {
hash1.set(ch, (hash1.get(ch) ?? 0) + 1);
}

for (const [k, v] of hash2) {
if ((hash1.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 hash2 = new Map<string, number>();

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

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

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

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

return true;
});
}
Loading