Skip to content

Commit 37e529a

Browse files
authored
Refactor wordSubsets function implementation
1 parent 3382362 commit 37e529a

File tree

4 files changed

+159
-87
lines changed

4 files changed

+159
-87
lines changed

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

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -218,63 +218,87 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
218218

219219
```ts
220220
function wordSubsets(words1: string[], words2: string[]): string[] {
221-
const cnt2 = new Map<string, number>();
222-
221+
const cnt: number[] = Array(26).fill(0);
223222
for (const b of words2) {
224-
const cnt = new Map<string, number>();
223+
const t: number[] = Array(26).fill(0);
225224
for (const c of b) {
226-
cnt.set(c, (cnt.get(c) ?? 0) + 1);
225+
t[c.charCodeAt(0) - 97]++;
227226
}
228-
229-
for (const [k, v] of cnt) {
230-
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
227+
for (let i = 0; i < 26; i++) {
228+
cnt[i] = Math.max(cnt[i], t[i]);
231229
}
232230
}
233231

234-
return words1.filter(a => {
235-
const cnt1 = new Map<string, number>();
232+
const ans: string[] = [];
233+
for (const a of words1) {
234+
const t: number[] = Array(26).fill(0);
236235
for (const c of a) {
237-
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
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+
}
238245
}
239246

240-
for (const [k, v] of cnt2) {
241-
if ((cnt1.get(k) ?? 0) < v) return false;
247+
if (ok) {
248+
ans.push(a);
242249
}
250+
}
243251

244-
return true;
245-
});
252+
return ans;
246253
}
247254
```
248255

249256
#### JavaScript
250257

251258
```js
252-
function wordSubsets(words1, words2) {
253-
const cnt2 = new Map();
254-
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+
255267
for (const b of words2) {
256-
const cnt = new Map();
268+
const t = Array(26).fill(0);
269+
257270
for (const c of b) {
258-
cnt.set(c, (cnt.get(c) ?? 0) + 1);
271+
t[c.charCodeAt(0) - 97]++;
259272
}
260-
261-
for (const [k, v] of cnt) {
262-
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
273+
274+
for (let i = 0; i < 26; i++) {
275+
cnt[i] = Math.max(cnt[i], t[i]);
263276
}
264277
}
265278

266-
return words1.filter(a => {
267-
const cnt1 = new Map();
279+
const ans = [];
280+
281+
for (const a of words1) {
282+
const t = Array(26).fill(0);
283+
268284
for (const c of a) {
269-
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
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+
}
270294
}
271295

272-
for (const [k, v] of cnt2) {
273-
if ((cnt1.get(k) ?? 0) < v) return false;
296+
if (ok) {
297+
ans.push(a);
274298
}
299+
}
275300

276-
return true;
277-
});
301+
return ans;
278302
}
279303
```
280304

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

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -213,63 +213,87 @@ func wordSubsets(words1 []string, words2 []string) (ans []string) {
213213

214214
```ts
215215
function wordSubsets(words1: string[], words2: string[]): string[] {
216-
const cnt2 = new Map<string, number>();
217-
216+
const cnt: number[] = Array(26).fill(0);
218217
for (const b of words2) {
219-
const cnt = new Map<string, number>();
218+
const t: number[] = Array(26).fill(0);
220219
for (const c of b) {
221-
cnt.set(c, (cnt.get(c) ?? 0) + 1);
220+
t[c.charCodeAt(0) - 97]++;
222221
}
223-
224-
for (const [k, v] of cnt) {
225-
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
222+
for (let i = 0; i < 26; i++) {
223+
cnt[i] = Math.max(cnt[i], t[i]);
226224
}
227225
}
228226

229-
return words1.filter(a => {
230-
const cnt1 = new Map<string, number>();
227+
const ans: string[] = [];
228+
for (const a of words1) {
229+
const t: number[] = Array(26).fill(0);
231230
for (const c of a) {
232-
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
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+
}
233240
}
234241

235-
for (const [k, v] of cnt2) {
236-
if ((cnt1.get(k) ?? 0) < v) return false;
242+
if (ok) {
243+
ans.push(a);
237244
}
245+
}
238246

239-
return true;
240-
});
247+
return ans;
241248
}
242249
```
243250

244251
#### JavaScript
245252

246253
```js
247-
function wordSubsets(words1, words2) {
248-
const cnt2 = new Map();
249-
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+
250262
for (const b of words2) {
251-
const cnt = new Map();
263+
const t = Array(26).fill(0);
264+
252265
for (const c of b) {
253-
cnt.set(c, (cnt.get(c) ?? 0) + 1);
266+
t[c.charCodeAt(0) - 97]++;
254267
}
255-
256-
for (const [k, v] of cnt) {
257-
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
268+
269+
for (let i = 0; i < 26; i++) {
270+
cnt[i] = Math.max(cnt[i], t[i]);
258271
}
259272
}
260273

261-
return words1.filter(a => {
262-
const cnt1 = new Map();
274+
const ans = [];
275+
276+
for (const a of words1) {
277+
const t = Array(26).fill(0);
278+
263279
for (const c of a) {
264-
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
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+
}
265289
}
266290

267-
for (const [k, v] of cnt2) {
268-
if ((cnt1.get(k) ?? 0) < v) return false;
291+
if (ok) {
292+
ans.push(a);
269293
}
294+
}
270295

271-
return true;
272-
});
296+
return ans;
273297
}
274298
```
275299

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
function wordSubsets(words1, words2) {
2-
const cnt2 = new Map();
3-
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+
49
for (const b of words2) {
5-
const cnt = new Map();
10+
const t = Array(26).fill(0);
11+
612
for (const c of b) {
7-
cnt.set(c, (cnt.get(c) ?? 0) + 1);
13+
t[c.charCodeAt(0) - 97]++;
814
}
9-
10-
for (const [k, v] of cnt) {
11-
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
15+
16+
for (let i = 0; i < 26; i++) {
17+
cnt[i] = Math.max(cnt[i], t[i]);
1218
}
1319
}
1420

15-
return words1.filter(a => {
16-
const cnt1 = new Map();
21+
const ans = [];
22+
23+
for (const a of words1) {
24+
const t = Array(26).fill(0);
25+
1726
for (const c of a) {
18-
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
27+
t[c.charCodeAt(0) - 97]++;
1928
}
2029

21-
for (const [k, v] of cnt2) {
22-
if ((cnt1.get(k) ?? 0) < v) return false;
30+
let ok = true;
31+
for (let i = 0; i < 26; i++) {
32+
if (cnt[i] > t[i]) {
33+
ok = false;
34+
break;
35+
}
2336
}
2437

25-
return true;
26-
});
38+
if (ok) {
39+
ans.push(a);
40+
}
41+
}
42+
43+
return ans;
2744
}
Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
function wordSubsets(words1: string[], words2: string[]): string[] {
2-
const cnt2 = new Map<string, number>();
3-
2+
const cnt: number[] = Array(26).fill(0);
43
for (const b of words2) {
5-
const cnt = new Map<string, number>();
4+
const t: number[] = Array(26).fill(0);
65
for (const c of b) {
7-
cnt.set(c, (cnt.get(c) ?? 0) + 1);
6+
t[c.charCodeAt(0) - 97]++;
87
}
9-
10-
for (const [k, v] of cnt) {
11-
cnt2.set(k, Math.max(cnt2.get(k) ?? 0, v));
8+
for (let i = 0; i < 26; i++) {
9+
cnt[i] = Math.max(cnt[i], t[i]);
1210
}
1311
}
1412

15-
return words1.filter(a => {
16-
const cnt1 = new Map<string, number>();
13+
const ans: string[] = [];
14+
for (const a of words1) {
15+
const t: number[] = Array(26).fill(0);
1716
for (const c of a) {
18-
cnt1.set(c, (cnt1.get(c) ?? 0) + 1);
17+
t[c.charCodeAt(0) - 97]++;
1918
}
2019

21-
for (const [k, v] of cnt2) {
22-
if ((cnt1.get(k) ?? 0) < v) return false;
20+
let ok = true;
21+
for (let i = 0; i < 26; i++) {
22+
if (cnt[i] > t[i]) {
23+
ok = false;
24+
break;
25+
}
2326
}
2427

25-
return true;
26-
});
28+
if (ok) {
29+
ans.push(a);
30+
}
31+
}
32+
33+
return ans;
2734
}

0 commit comments

Comments
 (0)