Skip to content

Commit 4f25cb2

Browse files
committed
Time: 12 ms (93.96%), Space: 56.2 MB (95.6%) - LeetHub
1 parent 1800084 commit 4f25cb2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string} chars
4+
* @return {number}
5+
*/
6+
var countCharacters = function(words, chars) {
7+
let freq = new Array(26).fill(0);
8+
const charToNum = (ch) => ch.charCodeAt(0) - 'a'.charCodeAt(0);
9+
for (const char of chars) {
10+
freq[charToNum(char)]++;
11+
}
12+
let totalLen = 0;
13+
for (const word of words) {
14+
let valid = true;
15+
let ref = [...freq];
16+
for (let i = 0; i < word.length; i++) {
17+
if (ref[charToNum(word[i])] <= 0) {
18+
valid = false;
19+
break;
20+
}
21+
ref[charToNum(word[i])]--;
22+
}
23+
if (valid) totalLen += word.length;
24+
}
25+
return totalLen;
26+
};

0 commit comments

Comments
 (0)