diff --git a/solution/0900-0999/0916.Word Subsets/README.md b/solution/0900-0999/0916.Word Subsets/README.md index 97f9454475748..d6f55da7cb725 100644 --- a/solution/0900-0999/0916.Word Subsets/README.md +++ b/solution/0900-0999/0916.Word Subsets/README.md @@ -214,6 +214,94 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) { } ``` +#### TypeScript + +```ts +function wordSubsets(words1: string[], words2: string[]): string[] { + const cnt: number[] = Array(26).fill(0); + for (const b of words2) { + const t: number[] = 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: string[] = []; + for (const a of words1) { + const t: number[] = Array(26).fill(0); + for (const c of a) { + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } + } + + if (ok) { + ans.push(a); + } + } + + return ans; +} +``` + +#### JavaScript + +```js +/** + * @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 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]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } + } + + if (ok) { + ans.push(a); + } + } + + 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 ff537e35eb96d..1b326bdfe3c43 100644 --- a/solution/0900-0999/0916.Word Subsets/README_EN.md +++ b/solution/0900-0999/0916.Word Subsets/README_EN.md @@ -209,6 +209,94 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) { } ``` +#### TypeScript + +```ts +function wordSubsets(words1: string[], words2: string[]): string[] { + const cnt: number[] = Array(26).fill(0); + for (const b of words2) { + const t: number[] = 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: string[] = []; + for (const a of words1) { + const t: number[] = Array(26).fill(0); + for (const c of a) { + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } + } + + if (ok) { + ans.push(a); + } + } + + return ans; +} +``` + +#### JavaScript + +```js +/** + * @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 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]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } + } + + if (ok) { + ans.push(a); + } + } + + return ans; +}; +``` + 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..a231afd792875 --- /dev/null +++ b/solution/0900-0999/0916.Word Subsets/Solution.js @@ -0,0 +1,44 @@ +/** + * @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 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]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } + } + + 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 new file mode 100644 index 0000000000000..132e2b7974dfc --- /dev/null +++ b/solution/0900-0999/0916.Word Subsets/Solution.ts @@ -0,0 +1,34 @@ +function wordSubsets(words1: string[], words2: string[]): string[] { + const cnt: number[] = Array(26).fill(0); + for (const b of words2) { + const t: number[] = 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: string[] = []; + for (const a of words1) { + const t: number[] = Array(26).fill(0); + for (const c of a) { + t[c.charCodeAt(0) - 97]++; + } + + let ok = true; + for (let i = 0; i < 26; i++) { + if (cnt[i] > t[i]) { + ok = false; + break; + } + } + + if (ok) { + ans.push(a); + } + } + + return ans; +}