A word can be formed from chars if every character in the word appears in chars with at least the same frequency. We first count character frequencies in chars, then for each word, count its character frequencies and verify that chars has enough of each character.
count for all characters in chars.cur_word for the word.cur_word has a count less than or equal to its count in count.res.Where is the length of , is the number of words and is the average length of each word.
Instead of building the complete frequency map for each word first, we can check character availability on the fly. As we iterate through each character of a word, we increment its count in a temporary map and immediately check if it exceeds the available count in chars. This allows early termination if a word is invalid.
count for all characters in chars.cur_word.cur_word.count, mark the word as invalid and break.res.class Solution:
def countCharacters(self, words: List[str], chars: str) -> int:
count = Counter(chars)
res = 0
for w in words:
cur_word = defaultdict(int)
good = True
for c in w:
cur_word[c] += 1
if cur_word[c] > count[c]:
good = False
break
if good:
res += len(w)
return resWhere is the length of , is the number of words and is the average length of each word.
Since we only have lowercase letters, we can use a fixed-size array of 26 elements instead of a hash map. This is more cache-friendly and avoids hash function overhead. We decrement counts as we use characters and reset the array for each new word using a stored original copy.
count of size 26 and populate it with frequencies from chars.count as org for resetting.count[c - 'a'].res.count to org for the next word.class Solution:
def countCharacters(self, words: List[str], chars: str) -> int:
count = [0] * 26
for c in chars:
count[ord(c) - ord('a')] += 1
org = count[:]
res = 0
for w in words:
good = True
for c in w:
i = ord(c) - ord('a')
count[i] -= 1
if count[i] < 0:
good = False
break
if good:
res += len(w)
for i in range(26):
count[i] = org[i]
return resWhere is the length of , is the number of words and is the average length of each word.