Skip to content

Commit 63357f5

Browse files
authored
Create 1160-find-words-that-can-be-formed-by-characters.js
1 parent 9faa775 commit 63357f5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string} chars
4+
* @return {number}
5+
*/
6+
const countCharacters = function(words, chars) {
7+
const cnt = Array(26)
8+
cnt.fill(0)
9+
for (let c of chars) cnt[c.charCodeAt() - 97]++
10+
11+
let res = 0
12+
for (let word of words) {
13+
let tmp = cnt.slice()
14+
let match = true
15+
for (let c of word) {
16+
if (--tmp[c.charCodeAt() - 97] < 0) {
17+
match = false
18+
break
19+
}
20+
}
21+
if (match) res += word.length
22+
}
23+
return res
24+
}

0 commit comments

Comments
 (0)