LeetCode - 1160. Find Words That Can Be Formed by Characters

henu·2024년 1월 10일
0

LeetCode

목록 보기
158/186

Solution

var countCharacters = function(words, chars) {
    const hash = {}

    for(char of chars) {
        hash[char] = (hash[char] || 0) + 1
    }

    return words.reduce((acc, cur) => {
        let copy = { ...hash }

        for(str of cur) {
            if(str in copy && copy[str] > 0) copy[str] -= 1
            else return acc
        }

        return acc + cur.length
    }, 0)
};

Explanation

0개의 댓글