Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ab1414

Browse files
authored
feat: add solutions to lc problem: No.0916 (doocs#3960)
1 parent 1bc9bd3 commit 8ab1414

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

‎solution/0900-0999/0916.Word Subsets/README.md‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,94 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
214214
}
215215
```
216216

217+
#### TypeScript
218+
219+
```ts
220+
function wordSubsets(words1: string[], words2: string[]): string[] {
221+
const cnt: number[] = Array(26).fill(0);
222+
for (const b of words2) {
223+
const t: number[] = Array(26).fill(0);
224+
for (const c of b) {
225+
t[c.charCodeAt(0) - 97]++;
226+
}
227+
for (let i = 0; i < 26; i++) {
228+
cnt[i] = Math.max(cnt[i], t[i]);
229+
}
230+
}
231+
232+
const ans: string[] = [];
233+
for (const a of words1) {
234+
const t: number[] = Array(26).fill(0);
235+
for (const c of a) {
236+
t[c.charCodeAt(0) - 97]++;
237+
}
238+
239+
let ok = true;
240+
for (let i = 0; i < 26; i++) {
241+
if (cnt[i] > t[i]) {
242+
ok = false;
243+
break;
244+
}
245+
}
246+
247+
if (ok) {
248+
ans.push(a);
249+
}
250+
}
251+
252+
return ans;
253+
}
254+
```
255+
256+
#### JavaScript
257+
258+
```js
259+
/**
260+
* @param {string[]} words1
261+
* @param {string[]} words2
262+
* @return {string[]}
263+
*/
264+
var wordSubsets = function (words1, words2) {
265+
const cnt = Array(26).fill(0);
266+
267+
for (const b of words2) {
268+
const t = Array(26).fill(0);
269+
270+
for (const c of b) {
271+
t[c.charCodeAt(0) - 97]++;
272+
}
273+
274+
for (let i = 0; i < 26; i++) {
275+
cnt[i] = Math.max(cnt[i], t[i]);
276+
}
277+
}
278+
279+
const ans = [];
280+
281+
for (const a of words1) {
282+
const t = Array(26).fill(0);
283+
284+
for (const c of a) {
285+
t[c.charCodeAt(0) - 97]++;
286+
}
287+
288+
let ok = true;
289+
for (let i = 0; i < 26; i++) {
290+
if (cnt[i] > t[i]) {
291+
ok = false;
292+
break;
293+
}
294+
}
295+
296+
if (ok) {
297+
ans.push(a);
298+
}
299+
}
300+
301+
return ans;
302+
};
303+
```
304+
217305
<!-- tabs:end -->
218306

219307
<!-- solution:end -->

‎solution/0900-0999/0916.Word Subsets/README_EN.md‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,94 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
209209
}
210210
```
211211

212+
#### TypeScript
213+
214+
```ts
215+
function wordSubsets(words1: string[], words2: string[]): string[] {
216+
const cnt: number[] = Array(26).fill(0);
217+
for (const b of words2) {
218+
const t: number[] = Array(26).fill(0);
219+
for (const c of b) {
220+
t[c.charCodeAt(0) - 97]++;
221+
}
222+
for (let i = 0; i < 26; i++) {
223+
cnt[i] = Math.max(cnt[i], t[i]);
224+
}
225+
}
226+
227+
const ans: string[] = [];
228+
for (const a of words1) {
229+
const t: number[] = Array(26).fill(0);
230+
for (const c of a) {
231+
t[c.charCodeAt(0) - 97]++;
232+
}
233+
234+
let ok = true;
235+
for (let i = 0; i < 26; i++) {
236+
if (cnt[i] > t[i]) {
237+
ok = false;
238+
break;
239+
}
240+
}
241+
242+
if (ok) {
243+
ans.push(a);
244+
}
245+
}
246+
247+
return ans;
248+
}
249+
```
250+
251+
#### JavaScript
252+
253+
```js
254+
/**
255+
* @param {string[]} words1
256+
* @param {string[]} words2
257+
* @return {string[]}
258+
*/
259+
var wordSubsets = function (words1, words2) {
260+
const cnt = Array(26).fill(0);
261+
262+
for (const b of words2) {
263+
const t = Array(26).fill(0);
264+
265+
for (const c of b) {
266+
t[c.charCodeAt(0) - 97]++;
267+
}
268+
269+
for (let i = 0; i < 26; i++) {
270+
cnt[i] = Math.max(cnt[i], t[i]);
271+
}
272+
}
273+
274+
const ans = [];
275+
276+
for (const a of words1) {
277+
const t = Array(26).fill(0);
278+
279+
for (const c of a) {
280+
t[c.charCodeAt(0) - 97]++;
281+
}
282+
283+
let ok = true;
284+
for (let i = 0; i < 26; i++) {
285+
if (cnt[i] > t[i]) {
286+
ok = false;
287+
break;
288+
}
289+
}
290+
291+
if (ok) {
292+
ans.push(a);
293+
}
294+
}
295+
296+
return ans;
297+
};
298+
```
299+
212300
<!-- tabs:end -->
213301

214302
<!-- solution:end -->
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {string[]} words1
3+
* @param {string[]} words2
4+
* @return {string[]}
5+
*/
6+
var wordSubsets = function (words1, words2) {
7+
const cnt = Array(26).fill(0);
8+
9+
for (const b of words2) {
10+
const t = Array(26).fill(0);
11+
12+
for (const c of b) {
13+
t[c.charCodeAt(0) - 97]++;
14+
}
15+
16+
for (let i = 0; i < 26; i++) {
17+
cnt[i] = Math.max(cnt[i], t[i]);
18+
}
19+
}
20+
21+
const ans = [];
22+
23+
for (const a of words1) {
24+
const t = Array(26).fill(0);
25+
26+
for (const c of a) {
27+
t[c.charCodeAt(0) - 97]++;
28+
}
29+
30+
let ok = true;
31+
for (let i = 0; i < 26; i++) {
32+
if (cnt[i] > t[i]) {
33+
ok = false;
34+
break;
35+
}
36+
}
37+
38+
if (ok) {
39+
ans.push(a);
40+
}
41+
}
42+
43+
return ans;
44+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function wordSubsets(words1: string[], words2: string[]): string[] {
2+
const cnt: number[] = Array(26).fill(0);
3+
for (const b of words2) {
4+
const t: number[] = Array(26).fill(0);
5+
for (const c of b) {
6+
t[c.charCodeAt(0) - 97]++;
7+
}
8+
for (let i = 0; i < 26; i++) {
9+
cnt[i] = Math.max(cnt[i], t[i]);
10+
}
11+
}
12+
13+
const ans: string[] = [];
14+
for (const a of words1) {
15+
const t: number[] = Array(26).fill(0);
16+
for (const c of a) {
17+
t[c.charCodeAt(0) - 97]++;
18+
}
19+
20+
let ok = true;
21+
for (let i = 0; i < 26; i++) {
22+
if (cnt[i] > t[i]) {
23+
ok = false;
24+
break;
25+
}
26+
}
27+
28+
if (ok) {
29+
ans.push(a);
30+
}
31+
}
32+
33+
return ans;
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /