-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
42 lines (40 loc) · 1.16 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @param {string[]} A
* @param {string[]} B
* @return {string[]}
*/
var wordSubsets = function (A, B) {
// 统计在B中每个字母在每个单词出现次数最大值
const localCountB = new Array(26).fill(0);
const countB = new Array(26).fill(0);
for (let i = 0; i < B.length; i++) {
const word = B[i];
for (let j = 0; j < word.length; j++) {
localCountB[word.charCodeAt(j) - 97]++;
}
for (let j = 0; j < 26; j++) {
countB[j] = Math.max(localCountB[j], countB[j]);
localCountB[j] = 0;
}
}
const result = [];
const countA = new Array(26).fill(0);
for (let i = 0; i < A.length; i++) {
// 统计A[i]中每个字母出现次数
const word = A[i];
for (let j = 0; j < word.length; j++) {
countA[word.charCodeAt(j) - 97]++;
}
// 判断是否是subset
let flag = true;
for (let j = 0; j < 26; j++) {
if (countA[j] < countB[j]) {
flag = false;
break;
}
}
flag && result.push(word);
countA.fill(0);
}
return result;
};