Open Tcdian opened 4 years ago
/**
* @param {string[]} words
* @param {string} chars
* @return {number}
*/
var countCharacters = function(words, chars) {
let result = 0;
const charsMap = new Array(26).fill(0);
for (let i = 0; i < chars.length; i++) {
charsMap[chars.charCodeAt(i) - 97] += 1;
}
words.forEach((word) => {
const wordMap = new Array(26).fill(0);
let isGoodString = true;
for (let i = 0; i < word.length; i++) {
wordMap[word.charCodeAt(i) - 97] += 1;
}
for (let i = 0; i < 26; i++) {
if (charsMap[i] < wordMap[i]) {
isGoodString = false;
break;
}
}
if (isGoodString) {
result += word.length;
}
});
return result;
};
1160. Find Words That Can Be Formed by Characters
给你一份『词汇表』(字符串数组)
words
和一张『字母表』(字符串)chars
。假如你可以用
chars
中的『字母』(字符)拼写出words
中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。注意:每次拼写时,
chars
中的每个字母都只能用一次。返回词汇表
words
中你掌握的所有单词的 长度之和。Example 1
Example 2
Note
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100