[
string
counter
hash table
]
Leetcode 1160. Find Words That Can Be Formed by Characters
Problem statement
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/
Solution
Just use counters to check that we can create word.
Complexity
It is O(n + m)
where n
is total length of words in words
and m
is a length of chars
.
Code
class Solution:
def countCharacters(self, words, chars):
cnt = Counter(chars)
return sum(len(w) for w in words if not Counter(w) - cnt)