/** * @param {string[]}words * @param {string}chars * @return {number} */ var countCharacters = function (words, chars) { const getMap = (s) => { const map = {}; for (const char of s) map[char] ? map[char]++ : (map[char] = 1); return map; };
const charMap = getMap(chars); let res = 0; for (const word of words) { const tempMap = Object.assign({}, charMap); let flag = true; for (const arr of word) { if (!tempMap[arr]) { flag = false; break; } tempMap[arr] -= 1; } if (flag) res += word.length; } return res; };
/** * @param {string[]}words * @param {string}chars * @return {number} */ var countCharacters = function (words, chars) { let res = 0; const arr = newArray(26).fill(0); for (let i = 0; i < chars.length; i++) arr[chars.charCodeAt(i) - 97]++; for (const word of words) { const tempArr = arr.concat(); let flag = true; for (let i = 0; i < word.length; i++) { if (!tempArr[word.charCodeAt(i) - 97]) flag = false; tempArr[word.charCodeAt(i) - 97]--; } if (flag) res += word.length; } return res; };