You are given an array of strings words and a string chars.
A string is good if it can be formed by characters from chars (each character can only be used once).
Return the sum of lengths of all good strings in words.
 
Example 1:
Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation: 
The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.

Example 2:
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation: 
The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.

 
Note:

1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
All strings contain lowercase English letters only.

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        

Solutions:
class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d = {}
		for i in chars:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		l = 0
		for i in words:
			flag = True
			for j in i:
				if j in d:
					if i.count(j) > d[j]:
						flag = False
						break
				else:
					flag = False
					break
			if flag:
				l += len(i)
		return l


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charBins = {char: chars.count(char) for char in chars}
		total = 0
		for word in words:
			if len(word) > len(chars):
				continue
			if not set(word).issubset(chars):
				continue
			letterBins = {letter: word.count(letter) for letter in word}
			goodWord = True
			for letter in letterBins:
				if letterBins[letter] > charBins[letter]:
					goodWord = False
			if goodWord:
				total += len(word)
		return total


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		c = Counter(chars)
		ret = 0
		for word in words:
			if Counter(word) - c == Counter():
				ret += len(word)
		return ret


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		return sum((len(word) for word in words if collections.Counter(word) & collections.Counter(chars) == collections.Counter(word)))


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = 0
		chars_map = collections.Counter(chars)
		for word in words:
			if collections.Counter(word) == collections.Counter(word) & chars_map:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		num = 0
		for word in words:
			letter = str()
			counter = Counter(chars)
			for i in word:
				if i in list(counter.keys()) and counter[i] > 0:
					letter += i
					counter[i] -= 1
			if letter == word:
				print(letter)
				num += len(word)
		return num


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		sum = 0
		for i in words:
			check = True
			for s in i:
				if i.count(s) > chars.count(s):
					check = False
			if check == True:
				sum = sum + len(i)
		return sum


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		op = 0
		for ele in words:
			count = 0
			for i in ele:
				if chars.count(i) >= ele.count(i):
					count += 1
			if count == len(ele):
				op += len(ele)
		return op


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charsums = 0
		chars = sorted(chars)
		for word in words:
			wordsize = len(word)
			word = sorted(word)
			charcounter = 0
			while len(word) > 0 and charcounter < len(chars):
				if word[0] == chars[charcounter]:
					word.remove(word[0])
					charcounter += 1
				else:
					charcounter += 1
			if len(word) == 0:
				charsums += wordsize
		return charsums


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = 0
		f = 1
		for s in words:
			f = 1
			for c in s:
				if s.count(c) > chars.count(c):
					f = 0
			if f:
				print(s)
				count += len(s)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		output = 0
		add = True
		for word in words:
			for c in word:
				if word.count(c) > chars.count(c):
					add = False
			if add:
				output += len(word)
			else:
				add = True
		return output


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = 0
		n = len(chars)
		for word in words:
			ok_word = True
			copy = chars
			visit = []
			for i in range(n):
				visit.append(0)
			for letter in word:
				ok_letter = False
				for i in range(n):
					if visit[i] == 0 and letter == copy[i]:
						ok_letter = True
						visit[i] = 1
						break
				if not ok_letter:
					ok_word = False
					break
			if ok_word:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charBins = {char: chars.count(char) for char in chars}
		goodWords = []
		for word in words:
			if len(word) > len(chars):
				continue
			if not set(word).issubset(chars):
				continue
			letterBins = {letter: word.count(letter) for letter in word}
			goodWord = True
			for letter in letterBins:
				if letterBins[letter] > charBins[letter]:
					goodWord = False
			if goodWord:
				goodWords.append(word)
		return sum((len(word) for word in goodWords))


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		from collections import Counter
		res = 0
		for j in words:
			tmp = Counter(j) & Counter(chars)
			if tmp == Counter(j):
				res += len(j)
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		goodCount = 0
		for word in words:
			wordLen = len(word)
			letterCount = 0
			chars_list = list(chars)
			for letter in word:
				if letter in chars_list:
					letterCount += 1
					chars_list.remove(letter)
			if letterCount == wordLen:
				goodCount += letterCount
		return goodCount


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		suma = 0
		for word in words:
			cur_word = list(word)
			cur_len = len(word)
			for c in chars:
				if c in cur_word:
					cur_word.remove(c)
			if len(cur_word) == 0:
				suma += cur_len
		return suma


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		from collections import Counter
		cd = Counter(chars)
		for w in words:
			wd = Counter(w)
			if len(wd & cd) and len(list((wd & cd).elements())) == len(w):
				ans += len(w)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		start = 0
		for i in words:
			count = 0
			t = list(chars)
			for j in i:
				if j in t:
					count += 1
					t.remove(j)
			if count == len(i):
				start += count
		return start


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		total = 0
		for word in words:
			mod_chars = list(chars)
			found = 0
			for c in word:
				if c in mod_chars:
					p = mod_chars.index(c)
					del mod_chars[p]
					found += 1
			if found == len(word):
				total += found
		return total


class Solution:

	def countCharacters(self, words, chars: str) -> int:
		myd = {}
		for i in chars:
			if hash(i) in list(myd.keys()):
				myd[hash(i)] += 1
			else:
				myd[hash(i)] = 1
		wdata = []
		for i in range(0, len(words)):
			x = dict()
			for letter in words[i]:
				if hash(letter) in list(x.keys()):
					x[hash(letter)] += 1
				else:
					x[hash(letter)] = 1
			wdata.append(x)
		count = 0
		for data in wdata:
			allin = True
			lcount = 0
			for letter in list(data.keys()):
				if letter in list(myd.keys()):
					if data[letter] > myd[letter]:
						allin = False
					else:
						lcount += data[letter]
				else:
					allin = False
			if allin == True:
				count += lcount
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		for word in words:
			charscopy = [i for i in chars]
			length = 0
			for char in word:
				if char in charscopy:
					length += 1
					charscopy.remove(char)
			if length == len(word):
				ans += length
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		char_counter = Counter(chars)
		return sum([len(word) for word in words if len(char_counter - Counter(word)) > 1 and len(Counter(word) - char_counter) == 0])


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d = Counter(chars)
		ans = 0
		for word in words:
			cur = Counter(word)
			if cur == cur & d:
				ans += len(word)
		return ans


import copy

class Solution:

	def countCharacters(self, w: List[str], c: str) -> int:
		d = {}
		for i in c:
			d[i] = d.get(i, 0) + 1
		ans = 0
		for i in range(len(w)):
			x = copy.deepcopy(d)
			f = True
			for j in w[i]:
				if j in x and x[j] > 0:
					x[j] -= 1
				else:
					f = False
					break
			if f:
				ans += len(w[i])
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		r = 0
		a = list(chars)
		for i in words:
			b = a.copy()
			s = False
			for j in i:
				if j not in b:
					s = True
				else:
					b.remove(j)
			if s != True:
				r += len(i)
		return r


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		char_set = collections.Counter(chars)
		N = len(chars)
		result = 0
		for i in words:
			if len(i) <= N and self.check(char_set, i):
				result += len(i)
		return result

	def check(self, char_ctr, S):
		S_ctr = collections.Counter(S)
		for i in S_ctr:
			if i not in char_ctr or S_ctr[i] > char_ctr[i]:
				return False
		return True


import copy

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charmap = dict()
		for c in chars:
			if c in charmap:
				charmap[c] += 1
			else:
				charmap[c] = 1
		size = 0
		for word in words:
			temp = copy.deepcopy(charmap)
			flag = True
			for c in word:
				if c in temp and temp[c] > 0:
					temp[c] -= 1
				else:
					flag = False
					break
			if flag:
				size += len(word)
		return size


import copy

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		tracker = dict()
		result = 0
		for s in chars:
			tracker.setdefault(s, 0)
			tracker[s] += 1
		for word in words:
			_temp = copy.deepcopy(tracker)
			for ch in word:
				if ch not in _temp:
					break
				if _temp[ch] <= 0:
					break
				_temp[ch] -= 1
			else:
				result += len(word)
		return result


import copy

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		s = dict()
		for c in chars:
			if c not in s:
				s[c] = 0
			s[c] += 1
		t = 0
		for word in words:
			s_copy = copy.deepcopy(s)
			valid = True
			for letter in word:
				if letter not in s_copy:
					valid = False
					break
				else:
					s_copy[letter] -= 1
					if s_copy[letter] == 0:
						del s_copy[letter]
			if valid:
				t += len(word)
		return t


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		c1 = Counter(chars)
		ans = 0
		for i in words:
			c2 = Counter(i)
			print(c2 - c1)
			if not c2 - c1:
				ans += len(i)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		import copy
		total = 0
		y = {}
		for char in chars:
			if char in y:
				y[char] += 1
			else:
				y[char] = 1
		for word in words:
			x = copy.deepcopy(y)
			temp = 0
			for char in word:
				if char in x and x[char] > 0:
					x[char] -= 1
					temp += 1
				else:
					temp = 0
					break
			total += temp
		return total


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		out = 0
		for word in words:
			good = True
			for char in word:
				if word.count(char) > chars.count(char):
					good = False
			if good:
				out += len(word)
		return out


import collections

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = 0
		for w in words:
			letters = list(set(w))
			isin = True
			for l in letters:
				if l not in chars or w.count(l) > chars.count(l):
					isin = False
					break
			if isin:
				count += len(w)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		a = 0
		for word in words:
			res = True
			for i in word:
				if word.count(i) > chars.count(i):
					res = False
					break
			if res:
				a += len(word)
		return a


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		res = 0
		dtar = Counter(chars)
		for i in range(len(words)):
			for j in words[i]:
				if j not in chars:
					break
				if words[i].count(j) > 1:
					if words[i].count(j) > dtar[j]:
						break
			else:
				res += len(words[i])
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d = Counter(chars)
		ans = 0
		for w in words:
			temp = d.copy()
			b = True
			for i in w:
				try:
					if temp[i] != 0:
						temp[i] -= 1
					else:
						b = False
						break
				except:
					b = False
					break
			if b:
				ans += len(w)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = 0
		freq = {}
		for n in range(len(chars)):
			if chars[n] not in freq.keys():
				freq[chars[n]] = 1
			else:
				freq[chars[n]] += 1
		for word in words:
			word_fq = {}
			is_match = True
			for m in range(len(word)):
				if word[m] not in freq.keys():
					is_match = False
					break
				if word[m] not in word_fq.keys():
					word_fq[word[m]] = 1
				else:
					word_fq[word[m]] += 1
			if is_match:
				is_fit = True
				for key in word_fq.keys():
					if key not in freq.keys():
						is_fit = False
						break
					if word_fq[key] > freq[key]:
						is_fit = False
						break
				if is_fit:
					count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		c = collections.Counter(chars)
		res = 0

		def valid(word):
			s = collections.Counter(word)
			for ch in s:
				if ch not in c or s[ch] > c[ch]:
					return False
			return True
		for word in words:
			if valid(word):
				res += len(word)
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		mp = Counter(chars)
		for word in words:
			ok = True
			mp_word = Counter(word)
			for (ch, f) in mp_word.items():
				if ch not in mp or mp_word[ch] > mp[ch]:
					ok = False
					break
			if ok:
				ans += len(word)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		from collections import Counter
		ans = 0
		chars_set = set(chars)
		count0 = Counter(chars)
		for word in words:
			count = Counter(word)
			if all((s in chars_set and count[s] <= count0[s] for s in word)):
				ans += len(word)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d_chars = Counter(chars)
		ans = 0
		for w in words:
			d_w = Counter(w)
			for (k, v) in d_w.items():
				if d_chars[k] < v:
					break
			else:
				ans += len(w)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		(sum, ct) = (0, collections.Counter)
		chars_counter = ct(chars)
		for word in words:
			word_counter = ct(word)
			if all((word_counter[c] <= chars_counter[c] for c in word_counter)):
				sum += len(word)
		return sum


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		c_dict = dict(Counter(chars))
		count = 0
		for word in words:
			word_d = dict(Counter(word))
			match = True
			for (k, v) in list(word_d.items()):
				if k not in c_dict or v > c_dict[k]:
					match = False
					break
			if match:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		char_sum = 0

		def isValid(word, chars):
			for letter in word:
				if chars[letter] > 0:
					chars[letter] -= 1
				else:
					return False
			return True
		for word in words:
			counter = Counter(chars)
			if isValid(word, counter):
				char_sum += len(word)
		return char_sum


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		tot = 0
		for w in words:
			d = {}
			for c in chars:
				if c in d:
					d[c] += 1
				else:
					d[c] = 1
			temp = 0
			for l in w:
				if l in d and d[l] > 0:
					d[l] -= 1
					temp += 1
				else:
					temp = 0
					break
			tot += temp
		return tot


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		for w in words:
			tc = chars
			flag = True
			for l in w:
				if l in tc:
					tc = tc.replace(l, '', 1)
				else:
					flag = False
			if flag:
				ans += len(w)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d = {}
		c = 0
		for i in chars:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in words:
			dd = {}
			for j in i:
				if j in dd:
					dd[j] += 1
				else:
					dd[j] = 1
			c1 = 0
			for x in dd.keys():
				if x in d:
					if d[x] >= dd[x]:
						c1 += dd[x]
					else:
						c1 = 0
						break
				else:
					c1 = 0
					break
			c += c1
		return c


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charsDict = {}
		for char in chars:
			if char not in charsDict:
				charsDict[char] = 0
			charsDict[char] += 1
		ans = 0
		for word in words:
			tempDict = charsDict.copy()
			isGood = True
			for char in word:
				if char not in tempDict:
					isGood = False
					continue
				elif tempDict[char] == 0:
					isGood = False
					continue
				else:
					tempDict[char] -= 1
			if isGood:
				ans += len(word)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		cCounter = collections.Counter(chars)
		sum = 0
		for word in words:
			wCounter = collections.Counter(word)
			match = True
			for (k, v) in wCounter.items():
				if cCounter[k] < v:
					match = False
			if match:
				sum += len(word)
		return sum


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		c1 = [0] * 256
		for c in chars:
			c1[ord(c)] += 1
		res = 0
		for word in words:
			if len(chars) < len(word):
				continue
			c2 = [0] * 256
			for c in word:
				c2[ord(c)] += 1
			goodStr = True
			for i in range(256):
				if c1[i] < c2[i]:
					goodStr = False
					break
			if goodStr:
				res += len(word)
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:

		def get_freq(input):
			freq = {}
			for c in input:
				if c not in freq:
					freq[c] = 0
				freq[c] += 1
			return freq

		def can_contain(freq_source, freq_word):
			for (key, item) in freq_word.items():
				if key not in freq_source or freq_source[key] < item:
					return False
			return True
		total_length = 0
		freq_source = get_freq(chars)
		for word in words:
			freq_word = get_freq(word)
			if can_contain(freq_source, freq_word):
				total_length += len(word)
		return total_length


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charsDict = {}
		for char in chars:
			if char not in charsDict:
				charsDict[char] = 0
			charsDict[char] += 1
		ans = 0
		for word in words:
			wordDict = {}
			for char in word:
				if char not in wordDict:
					wordDict[char] = 0
				wordDict[char] += 1
			isGood = True
			for (key, val) in list(wordDict.items()):
				if key not in charsDict:
					isGood = False
					break
				elif val > charsDict[key]:
					isGood = False
					break
			if isGood:
				ans += len(word)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		res = 0
		d = dict()
		for char in chars:
			d[char] = d.get(char, 0) + 1
		for word in words:
			c = dict()
			for char in word:
				c[char] = c.get(char, 0) + 1
			bad = False
			while c and (not bad):
				char = c.popitem()
				if char[0] in d and d[char[0]] >= char[1]:
					continue
				else:
					bad = True
			if not bad:
				res += len(word)
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		out = 0
		chars = list(chars)
		for i in words:
			f = 0
			temp = chars[:]
			for j in i:
				if j in temp:
					temp.remove(j)
				else:
					f = 1
					break
			if f == 0:
				out += len(i)
		return out


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		register = {}
		for char in chars:
			register[char] = register.get(char, 0) + 1
		result = 0
		for word in words:
			temp = {}
			for char in word:
				temp[char] = temp.get(char, 0) + 1
			for (char, num) in list(temp.items()):
				if temp[char] > register.get(char, 0):
					break
			else:
				result += len(word)
		return result


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		charChars = Counter(chars)
		counter = 0
		for word in words:
			countC = Counter(word)
			count = 0
			for letter in countC.items():
				if letter[0] in charChars and charChars[letter[0]] >= letter[1]:
					count += 1
			if count == len(countC):
				counter += len(word)
		return counter


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		dic_char = {}
		for i in chars:
			if i not in dic_char:
				dic_char[i] = 1
			else:
				dic_char[i] += 1
		cnt = 0
		for i in words:
			dic_word = {}
			for j in i:
				if j not in dic_word:
					dic_word[j] = 1
				else:
					dic_word[j] += 1
			print(dic_word)
			for (k, v) in dic_word.items():
				if k not in dic_char or dic_char[k] < v:
					break
			else:
				cnt += len(i)
		return cnt


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:

		def strToList(word):
			return [char for char in word]

		def canForm(word, bank):
			tmp = word
			while tmp != []:
				x = tmp[0]
				tmp.remove(tmp[0])
				if x in bank:
					bank.remove(x)
				else:
					return False
			return True
		totalLen = 0
		for word in words:
			bank = strToList(chars)
			wordAsList = strToList(word)
			if canForm(wordAsList, bank):
				totalLen += len(word)
		return totalLen


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		setChars = set(chars)
		counts = [0] * len(setChars)
		map = {}
		res = 0
		for (i, val) in enumerate(setChars):
			map[val] = i
		for (i, val) in enumerate(chars):
			counts[map.get(val)] += 1
		for word in words:
			tempCounts = counts[:]
			flag = 1
			for char in word:
				index = map.get(char, -1)
				if index == -1:
					flag = 0
					continue
				tempCounts[index] -= 1
				if tempCounts[index] < 0:
					flag = 0
					continue
			if flag:
				res += len(word)
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d = {}
		for i in range(len(chars)):
			if chars[i] not in d:
				d[chars[i]] = 1
			else:
				d[chars[i]] += 1
		c = 0
		for i in range(len(words)):
			di = {}
			for j in range(len(words[i])):
				if words[i][j] not in di:
					di[words[i][j]] = 1
				else:
					di[words[i][j]] += 1
			l = list(di.keys())
			temp = 0
			for j in range(len(l)):
				if l[j] not in d:
					temp = 1
					break
				elif di[l[j]] > d[l[j]]:
					temp = 1
					break
				else:
					temp = 0
			if temp == 0:
				c += len(words[i])
		return c


class Solution:

	def countCharacters(self, words, chars):
		sum = 0
		count = {}
		for c in chars:
			if c in count:
				count[c] += 1
			else:
				count[c] = 1
		for word in words:
			seen = {}
			validWord = True
			for c in word:
				if c in seen:
					seen[c] += 1
				else:
					seen[c] = 1
				if c not in count or seen[c] > count[c]:
					validWord = False
			if validWord:
				sum += len(word)
		return sum


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		return bruteforce_with_counter(words, chars)
from collections import Counter

def bruteforce_with_counter(words, chars):
	counter = Counter(chars)
	return sum((len(w) for w in words if not Counter(w) - counter))


from collections import Counter as di

class Solution:

	def countCharacters(self, a: List[str], c: str) -> int:
		d = di(c)
		ans = 0
		for i in a:
			d1 = di(i)
			if len(d1 - d) == 0:
				ans += sum(d1.values())
		return ans


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		letters = Counter(chars)
		out = 0
		for word in words:
			length = len(word)
			if not Counter(word) - letters:
				out += length
		return out


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:

		def issubset(wc, cc):
			for x in wc.keys():
				cc[x] = cc.get(x, 0) - wc[x]
			for x in cc.keys():
				if cc[x] < 0:
					return False
			return True
		res = 0
		cc = Counter(chars)
		for word in words:
			wc = Counter(word)
			if issubset(wc, cc.copy()):
				res += len(word)
		return res


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		char_count = collections.Counter(chars)
		total = 0
		for word in words:
			if not collections.Counter(word) - char_count:
				total += len(word)
		return total


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		d = Counter(chars)
		cnt = [Counter(w) for w in words]
		ans = 0
		for (i, x) in enumerate(cnt):
			found = True
			print(x)
			for k in x:
				if k not in d or x[k] > d[k]:
					found = False
					break
			if found:
				ans += len(words[i])
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		chars = Counter(chars)
		return sum((len(w) for w in words if not Counter(w) - chars))


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = Counter(chars)
		out = 0
		for word in words:
			curr = Counter(word)
			if not curr - count:
				out += len(word)
		return out


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		c_c = Counter(chars)
		good_w = []
		for w in words:
			w_c = Counter(w)
			sampled_w = ''.join([c for c in w if c_c[c] >= w_c[c]])
			if w == sampled_w:
				good_w.append(w)
		return sum((len(w) for w in good_w))


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		char_map = {c: chars.count(c) for c in set(chars)}
		count = 0
		for w in words:
			good = True
			for c in w:
				if c not in char_map.keys() or w.count(c) > char_map[c]:
					good = False
			if good:
				count += len(w)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		letters = {}
		for char in chars:
			letters[char] = letters.get(char, 0) + 1
		count = 0
		for word in words:
			tmp = letters.copy()
			flag = 1
			for char in word:
				if char in tmp and tmp[char] >= 1:
					tmp[char] -= 1
				else:
					flag = 0
					break
			if flag:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		for word in words:
			c = collections.Counter(chars)
			add = True
			for letter in word:
				if c[letter] > 0:
					c[letter] -= 1
				else:
					add = False
			if add == True:
				ans += len(word)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		cmap = {ch: chars.count(ch) for ch in chars}
		for word in words:
			wmap = {ch: word.count(ch) for ch in word}
			count_me_in = True
			for (k, v) in wmap.items():
				try:
					v1 = cmap[k]
					if v1 < v:
						raise Exception('Frequency not enough!')
				except:
					count_me_in = False
					break
			if count_me_in:
				ans += len(word)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		char_count = collections.Counter(chars)
		good_str_len = 0
		for word in words:
			temp = char_count.copy()
			temp_str_len = 0
			for ch in word:
				if ch in temp and temp[ch] > 0:
					temp_str_len += 1
					temp[ch] -= 1
			if temp_str_len == len(word):
				good_str_len += len(word)
		return good_str_len


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		if len(chars) == 0 or len(words) == 0:
			return 0
		letterCounts = {}
		for c in chars:
			if str(c) in letterCounts:
				letterCounts[str(c)] += 1
			else:
				letterCounts[str(c)] = 1
		totalLength = 0
		for word in words:
			currentLetterCounts = {}
			for letter in word:
				if str(letter) in currentLetterCounts:
					currentLetterCounts[str(letter)] += 1
				else:
					currentLetterCounts[str(letter)] = 1
			valid = True
			for (key, value) in currentLetterCounts.items():
				if key not in letterCounts:
					valid = False
					break
				if letterCounts[key] < value:
					valid = False
					break
			if valid:
				totalLength += len(word)
		return totalLength


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		chars_number = collections.Counter(chars)
		for w in words:
			word_number = collections.Counter(w)
			print(word_number)
			for i in word_number:
				print(word_number[i])
				if word_number[i] > chars_number[i]:
					break
			else:
				ans += len(w)
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		flag = [0 for i in range(len(words))]
		total = 0
		for word in words:
			dic = {}
			for char in word:
				if char in dic:
					dic[char] += 1
				else:
					dic[char] = 1
			count = 0
			for i in range(len(chars)):
				if chars[i] in dic and dic[chars[i]] > 0:
					dic[chars[i]] -= 1
					count += 1
			good = True
			for char in dic:
				if dic[char] > 0:
					good = False
					break
			if good:
				total += count
		return total


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		myd = {}
		count = 0
		for i in chars:
			if i in myd.keys():
				myd[i] += 1
			else:
				myd[i] = 1
		for word in words:
			allin = True
			for letter in word:
				if letter in myd.keys():
					if word.count(letter) > myd[letter]:
						allin = False
				else:
					allin = False
			if allin == True:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		good_words_len_sum = 0
		counter = {}
		for char in chars:
			if not char in counter:
				counter[char] = 1
			else:
				counter[char] += 1
		for word in words:
			my_counter = counter.copy()
			for char in word:
				if my_counter.get(char, 0) > 0:
					my_counter[char] -= 1
				else:
					break
			else:
				good_words_len_sum += len(word)
		return good_words_len_sum


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		length = 0
		m = Counter(chars)
		for word in words:
			passed = True
			for char in word:
				if m[char] < word.count(char):
					passed = False
			if passed:
				length += len(word)
		return length


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		from collections import Counter as cnt
		return sum([not cnt(word) - cnt(chars) and len(word) for word in words])


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		count = 0
		dic = collections.Counter(chars)
		for word in words:
			passed = True
			for ch in word:
				if dic[ch] < word.count(ch):
					passed = False
			if passed:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		counter = 0
		for i in words:
			a = i
			for j in chars:
				if j in a:
					a = a[:a.index(j)] + a[a.index(j) + 1:]
			if len(a) == 0:
				counter += len(i)
		return counter


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		words = [collections.Counter(word) for word in words]
		c = collections.Counter(chars)
		print(words)
		print(c)
		return sum([sum(list(w.values())) for w in words if all([c[i] >= w[i] for i in w])])


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		freq = defaultdict(int)
		for l in chars:
			freq[l] += 1
		count = 0
		for word in words:
			testFreq = freq.copy()
			match = True
			for letter in word:
				if testFreq[letter] <= 0:
					match = False
					break
				testFreq[letter] -= 1
			if match:
				count += len(word)
		return count


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		tot = 0
		totchars = {}
		for char in chars:
			if char in totchars:
				totchars[char] += 1
			else:
				totchars[char] = 1
		for word in words:
			word = sorted(word)
			works = True
			i = 0
			while i < len(word):
				count = 0
				while word[i + count] == word[i]:
					count += 1
					if i + count == len(word):
						break
				if word[i] in totchars:
					if count > totchars[word[i]]:
						works = False
				else:
					works = False
				i += count
			if works:
				tot += len(word)
		return tot


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		sum = 0
		p = 0
		for th in words:
			for i in range(len(th)):
				if th[i] in chars:
					if th.count(th[i]) <= chars.count(th[i]):
						p = p + 1
			if p == len(th):
				sum = sum + p
			p = 0
		return sum


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		ans = 0
		for i in range(len(words)):
			if Counter(words[i]) & Counter(chars) == Counter(words[i]):
				ans += len(words[i])
		return ans


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		from collections import Counter
		sum_ = 0
		for word in words:
			s = Counter(chars)
			flag = 0
			for letter in word:
				if letter in list(s.keys()) and s[letter] != 0:
					s[letter] -= 1
				else:
					flag = 1
			if flag == 0:
				sum_ += len(word)
		return sum_


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		out = 0
		for word in words:
			cnt = 0
			for letter in word:
				if letter in chars and word.count(letter) <= chars.count(letter):
					cnt += 1
			if cnt == len(word):
				out += len(word)
		return out


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		letters = Counter(chars)
		c = 0
		for w in words:
			if Counter(chars) & Counter(w) == Counter(w):
				c += len(w)
		return c


import collections

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		n = 0
		for word in words:
			count = 0
			for letter in word:
				if letter in chars and word.count(letter) <= chars.count(letter):
					count += 1
			if count == len(word):
				n += len(word)
		return n


class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		res = []
		for word in words:
			count = 0
			for char in word:
				if char in chars and word.count(char) <= chars.count(char):
					count += 1
			if count == len(word):
				res.append(count)
		return sum(res)


from collections import Counter

class Solution:

	def countCharacters(self, words: List[str], chars: str) -> int:
		r = Counter(chars)
		return sum([len(word) if Counter(word) - r == Counter() else 0 for word in words])