From 29630981c1844b0ca63959f97e33f842cd8ca0b5 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 10 Jan 2025 22:47:35 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.0916 --- .../0900-0999/0916.Word Subsets/README.md | 64 +++++++++++++++++++ .../0900-0999/0916.Word Subsets/README_EN.md | 64 +++++++++++++++++++ .../0900-0999/0916.Word Subsets/Solution.js | 27 ++++++++ .../0900-0999/0916.Word Subsets/Solution.ts | 27 ++++++++ 4 files changed, 182 insertions(+) create mode 100644 solution/0900-0999/0916.Word Subsets/Solution.js create mode 100644 solution/0900-0999/0916.Word Subsets/Solution.ts diff --git a/solution/0900-0999/0916.Word Subsets/README.md b/solution/0900-0999/0916.Word Subsets/README.md index 551a6413d4ffc..4ce452b883580 100644 --- a/solution/0900-0999/0916.Word Subsets/README.md +++ b/solution/0900-0999/0916.Word Subsets/README.md @@ -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(); + + 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; + }); +} +``` + +#### 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; + }); +} +``` + diff --git a/solution/0900-0999/0916.Word Subsets/README_EN.md b/solution/0900-0999/0916.Word Subsets/README_EN.md index 74f89848a05c4..032633b2450a5 100644 --- a/solution/0900-0999/0916.Word Subsets/README_EN.md +++ b/solution/0900-0999/0916.Word Subsets/README_EN.md @@ -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(); + + 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; + }); +} +``` + +#### 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; + }); +} +``` + diff --git a/solution/0900-0999/0916.Word Subsets/Solution.js b/solution/0900-0999/0916.Word Subsets/Solution.js new file mode 100644 index 0000000000000..2b4de01b9dc8c --- /dev/null +++ b/solution/0900-0999/0916.Word Subsets/Solution.js @@ -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; + }); +} diff --git a/solution/0900-0999/0916.Word Subsets/Solution.ts b/solution/0900-0999/0916.Word Subsets/Solution.ts new file mode 100644 index 0000000000000..85e59072045f3 --- /dev/null +++ b/solution/0900-0999/0916.Word Subsets/Solution.ts @@ -0,0 +1,27 @@ +function wordSubsets(words1: string[], words2: string[]): string[] { + 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; + }); +}