From 3cb6db85f6c8a8b6e783f7c37600904bf7960127 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 10 Jan 2025 22:47:35 +0300 Subject: [PATCH 1/3] 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; + }); +} From d085e5f755f07b7dac8f25a1caac348a52b0b6d7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 8 Apr 2025 12:37:23 +0800 Subject: [PATCH 2/3] Refactor wordSubsets function implementation --- .../0900-0999/0916.Word Subsets/README.md | 82 ++++++++++++------- .../0900-0999/0916.Word Subsets/README_EN.md | 82 ++++++++++++------- .../0900-0999/0916.Word Subsets/Solution.js | 47 +++++++---- .../0900-0999/0916.Word Subsets/Solution.ts | 35 ++++---- 4 files changed, 159 insertions(+), 87 deletions(-) diff --git a/solution/0900-0999/0916.Word Subsets/README.md b/solution/0900-0999/0916.Word Subsets/README.md index 27af7dc52e7b1..fc2b44ff4822c 100644 --- a/solution/0900-0999/0916.Word Subsets/README.md +++ b/solution/0900-0999/0916.Word Subsets/README.md @@ -218,63 +218,87 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) { ```ts function wordSubsets(words1: string[], words2: string[]): string[] { - const cnt2 = new Map(); - + const cnt: number[] = Array(26).fill(0); for (const b of words2) { - const cnt = new Map(); + const t: number[] = Array(26).fill(0); for (const c of b) { - cnt.set(c, (cnt.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - - for (const [k, v] of cnt) { - cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v)); + for (let i = 0; i < 26; i++) { + cnt[i] = Math.max(cnt[i], t[i]); } } - return words1.filter(a => { - const cnt1 = new Map(); + const ans: string[] = []; + for (const a of words1) { + const t: number[] = Array(26).fill(0); for (const c of a) { - cnt1.set(c, (cnt1.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } } - for (const [k, v] of cnt2) { - if ((cnt1.get(k) ?? 0) < v) return false; + if (ok) { + ans.push(a); } + } - return true; - }); + return ans; } ``` #### JavaScript ```js -function wordSubsets(words1, words2) { - const cnt2 = new Map(); - +/** + * @param {string[]} words1 + * @param {string[]} words2 + * @return {string[]} + */ +var wordSubsets = function(words1, words2) { + const cnt = Array(26).fill(0); + for (const b of words2) { - const cnt = new Map(); + const t = Array(26).fill(0); + for (const c of b) { - cnt.set(c, (cnt.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - - for (const [k, v] of cnt) { - cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v)); + + for (let i = 0; i < 26; i++) { + cnt[i] = Math.max(cnt[i], t[i]); } } - return words1.filter(a => { - const cnt1 = new Map(); + const ans = []; + + for (const a of words1) { + const t = Array(26).fill(0); + for (const c of a) { - cnt1.set(c, (cnt1.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } } - for (const [k, v] of cnt2) { - if ((cnt1.get(k) ?? 0) < v) return false; + if (ok) { + ans.push(a); } + } - return true; - }); + return ans; } ``` diff --git a/solution/0900-0999/0916.Word Subsets/README_EN.md b/solution/0900-0999/0916.Word Subsets/README_EN.md index 11fe9c547ead6..f5d4c2ce4a1e9 100644 --- a/solution/0900-0999/0916.Word Subsets/README_EN.md +++ b/solution/0900-0999/0916.Word Subsets/README_EN.md @@ -213,63 +213,87 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) { ```ts function wordSubsets(words1: string[], words2: string[]): string[] { - const cnt2 = new Map(); - + const cnt: number[] = Array(26).fill(0); for (const b of words2) { - const cnt = new Map(); + const t: number[] = Array(26).fill(0); for (const c of b) { - cnt.set(c, (cnt.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - - for (const [k, v] of cnt) { - cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v)); + for (let i = 0; i < 26; i++) { + cnt[i] = Math.max(cnt[i], t[i]); } } - return words1.filter(a => { - const cnt1 = new Map(); + const ans: string[] = []; + for (const a of words1) { + const t: number[] = Array(26).fill(0); for (const c of a) { - cnt1.set(c, (cnt1.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } } - for (const [k, v] of cnt2) { - if ((cnt1.get(k) ?? 0) < v) return false; + if (ok) { + ans.push(a); } + } - return true; - }); + return ans; } ``` #### JavaScript ```js -function wordSubsets(words1, words2) { - const cnt2 = new Map(); - +/** + * @param {string[]} words1 + * @param {string[]} words2 + * @return {string[]} + */ +var wordSubsets = function(words1, words2) { + const cnt = Array(26).fill(0); + for (const b of words2) { - const cnt = new Map(); + const t = Array(26).fill(0); + for (const c of b) { - cnt.set(c, (cnt.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - - for (const [k, v] of cnt) { - cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v)); + + for (let i = 0; i < 26; i++) { + cnt[i] = Math.max(cnt[i], t[i]); } } - return words1.filter(a => { - const cnt1 = new Map(); + const ans = []; + + for (const a of words1) { + const t = Array(26).fill(0); + for (const c of a) { - cnt1.set(c, (cnt1.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } } - for (const [k, v] of cnt2) { - if ((cnt1.get(k) ?? 0) < v) return false; + if (ok) { + ans.push(a); } + } - return true; - }); + return ans; } ``` diff --git a/solution/0900-0999/0916.Word Subsets/Solution.js b/solution/0900-0999/0916.Word Subsets/Solution.js index 747f479d017f9..ba478e9c95860 100644 --- a/solution/0900-0999/0916.Word Subsets/Solution.js +++ b/solution/0900-0999/0916.Word Subsets/Solution.js @@ -1,27 +1,44 @@ -function wordSubsets(words1, words2) { - const cnt2 = new Map(); - +/** + * @param {string[]} words1 + * @param {string[]} words2 + * @return {string[]} + */ +var wordSubsets = function(words1, words2) { + const cnt = Array(26).fill(0); + for (const b of words2) { - const cnt = new Map(); + const t = Array(26).fill(0); + for (const c of b) { - cnt.set(c, (cnt.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - - for (const [k, v] of cnt) { - cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v)); + + for (let i = 0; i < 26; i++) { + cnt[i] = Math.max(cnt[i], t[i]); } } - return words1.filter(a => { - const cnt1 = new Map(); + const ans = []; + + for (const a of words1) { + const t = Array(26).fill(0); + for (const c of a) { - cnt1.set(c, (cnt1.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - for (const [k, v] of cnt2) { - if ((cnt1.get(k) ?? 0) < v) return false; + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } } - return true; - }); + if (ok) { + ans.push(a); + } + } + + return ans; } diff --git a/solution/0900-0999/0916.Word Subsets/Solution.ts b/solution/0900-0999/0916.Word Subsets/Solution.ts index 6a57b5be0b7df..132e2b7974dfc 100644 --- a/solution/0900-0999/0916.Word Subsets/Solution.ts +++ b/solution/0900-0999/0916.Word Subsets/Solution.ts @@ -1,27 +1,34 @@ function wordSubsets(words1: string[], words2: string[]): string[] { - const cnt2 = new Map(); - + const cnt: number[] = Array(26).fill(0); for (const b of words2) { - const cnt = new Map(); + const t: number[] = Array(26).fill(0); for (const c of b) { - cnt.set(c, (cnt.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - - for (const [k, v] of cnt) { - cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v)); + for (let i = 0; i < 26; i++) { + cnt[i] = Math.max(cnt[i], t[i]); } } - return words1.filter(a => { - const cnt1 = new Map(); + const ans: string[] = []; + for (const a of words1) { + const t: number[] = Array(26).fill(0); for (const c of a) { - cnt1.set(c, (cnt1.get(c) ?? 0) + 1); + t[c.charCodeAt(0) - 97]++; } - for (const [k, v] of cnt2) { - if ((cnt1.get(k) ?? 0) < v) return false; + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } } - return true; - }); + if (ok) { + ans.push(a); + } + } + + return ans; } From 3c119c0072d87c6e3d52cbef104fe96c68ad8484 Mon Sep 17 00:00:00 2001 From: yanglbme <21008209+yanglbme@users.noreply.github.com> Date: Tue, 8 Apr 2025 04:38:08 +0000 Subject: [PATCH 3/3] style: format code and docs with prettier --- solution/0900-0999/0916.Word Subsets/README.md | 14 +++++++------- solution/0900-0999/0916.Word Subsets/README_EN.md | 14 +++++++------- solution/0900-0999/0916.Word Subsets/Solution.js | 14 +++++++------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/solution/0900-0999/0916.Word Subsets/README.md b/solution/0900-0999/0916.Word Subsets/README.md index fc2b44ff4822c..d6f55da7cb725 100644 --- a/solution/0900-0999/0916.Word Subsets/README.md +++ b/solution/0900-0999/0916.Word Subsets/README.md @@ -261,26 +261,26 @@ function wordSubsets(words1: string[], words2: string[]): string[] { * @param {string[]} words2 * @return {string[]} */ -var wordSubsets = function(words1, words2) { +var wordSubsets = function (words1, words2) { const cnt = Array(26).fill(0); - + for (const b of words2) { const t = Array(26).fill(0); - + for (const c of b) { t[c.charCodeAt(0) - 97]++; } - + for (let i = 0; i < 26; i++) { cnt[i] = Math.max(cnt[i], t[i]); } } const ans = []; - + for (const a of words1) { const t = Array(26).fill(0); - + for (const c of a) { t[c.charCodeAt(0) - 97]++; } @@ -299,7 +299,7 @@ var wordSubsets = function(words1, words2) { } return ans; -} +}; ``` diff --git a/solution/0900-0999/0916.Word Subsets/README_EN.md b/solution/0900-0999/0916.Word Subsets/README_EN.md index f5d4c2ce4a1e9..1b326bdfe3c43 100644 --- a/solution/0900-0999/0916.Word Subsets/README_EN.md +++ b/solution/0900-0999/0916.Word Subsets/README_EN.md @@ -256,26 +256,26 @@ function wordSubsets(words1: string[], words2: string[]): string[] { * @param {string[]} words2 * @return {string[]} */ -var wordSubsets = function(words1, words2) { +var wordSubsets = function (words1, words2) { const cnt = Array(26).fill(0); - + for (const b of words2) { const t = Array(26).fill(0); - + for (const c of b) { t[c.charCodeAt(0) - 97]++; } - + for (let i = 0; i < 26; i++) { cnt[i] = Math.max(cnt[i], t[i]); } } const ans = []; - + for (const a of words1) { const t = Array(26).fill(0); - + for (const c of a) { t[c.charCodeAt(0) - 97]++; } @@ -294,7 +294,7 @@ var wordSubsets = function(words1, words2) { } return ans; -} +}; ``` diff --git a/solution/0900-0999/0916.Word Subsets/Solution.js b/solution/0900-0999/0916.Word Subsets/Solution.js index ba478e9c95860..a231afd792875 100644 --- a/solution/0900-0999/0916.Word Subsets/Solution.js +++ b/solution/0900-0999/0916.Word Subsets/Solution.js @@ -3,26 +3,26 @@ * @param {string[]} words2 * @return {string[]} */ -var wordSubsets = function(words1, words2) { +var wordSubsets = function (words1, words2) { const cnt = Array(26).fill(0); - + for (const b of words2) { const t = Array(26).fill(0); - + for (const c of b) { t[c.charCodeAt(0) - 97]++; } - + for (let i = 0; i < 26; i++) { cnt[i] = Math.max(cnt[i], t[i]); } } const ans = []; - + for (const a of words1) { const t = Array(26).fill(0); - + for (const c of a) { t[c.charCodeAt(0) - 97]++; } @@ -41,4 +41,4 @@ var wordSubsets = function(words1, words2) { } return ans; -} +};