# 40. 万能单词拼写-掌握单词个数
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on('line', function(num) {
input.push(num);
});
rl.on('close', () => {
const N = parseInt(input[0]);
const words = input.slice(1, N+1);
const chars = input[N+1];
const count = {};
let wildcards = 0;
for(let c of chars) {
if (c !== '?') {
if (count[c]) {
count[c] += 1;
} else {
count[c] = 1;
}
} else {
wildcards++;
}
}
let res = 0;
for(let word of words) {
const wordCount = {};
for(let c of word) {
if (wordCount[c]) {
wordCount[c] += 1;
} else {
wordCount[c] = 1;
}
}
if (canSpell(wordCount, count, wildcards)) {
res++;
}
}
console.log(res);
});
function canSpell(wordCount, count, wildcards) {
for(let key in wordCount) {
if (!count[key]) {
return false;
}
if (wordCount[key] > count[key]) {
wildcards -= wordCount[key] - count[key];
if (wildcard < 0) {
return false;
}
}
}
return true;
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56