Find Words That Can Be Formed by Characters

Problem Id: 1160 Difficulty: Easy



class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        ans = 0
        main = self.count(chars)
        for word in words:
            now = self.count(word)
            flag = True
            for c in string.ascii_lowercase:
                if main[c] < now[c]:
                    flag = False
            if flag:
                ans += len(word)
        return ans

    def count(self, word):
        return {c:word.count(c) for c in string.ascii_lowercase}