From 338236209055a491c5aa75dd81a46ca8a0df13f8 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 97f9454475748..27af7dc52e7b1 100644 --- a/solution/0900-0999/0916.Word Subsets/README.md +++ b/solution/0900-0999/0916.Word Subsets/README.md @@ -214,6 +214,70 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) { } ``` +#### TypeScript + +```ts +function wordSubsets(words1: string[], words2: string[]): string[] { + 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; + }); +} +``` + +#### 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; + }); +} +``` + diff --git a/solution/0900-0999/0916.Word Subsets/README_EN.md b/solution/0900-0999/0916.Word Subsets/README_EN.md index ff537e35eb96d..11fe9c547ead6 100644 --- a/solution/0900-0999/0916.Word Subsets/README_EN.md +++ b/solution/0900-0999/0916.Word Subsets/README_EN.md @@ -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(); + + 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; + }); +} +``` + +#### 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; + }); +} +``` + 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..747f479d017f9 --- /dev/null +++ b/solution/0900-0999/0916.Word Subsets/Solution.js @@ -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; + }); +} 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..6a57b5be0b7df --- /dev/null +++ b/solution/0900-0999/0916.Word Subsets/Solution.ts @@ -0,0 +1,27 @@ +function wordSubsets(words1: string[], words2: string[]): string[] { + 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; + }); +}