我想检查一个字符串是否可以由另一个字符串组成,例如,在下面的例子中,我想检查列表中有多少个字符串targets
可以由字符串组成chars
。 中的每个字符chars
只能使用一次。
targets = ["cat","bt","hat","tree"], chars = "atach"
我的代码如下:
ans = 0
chars_freq = Counter(chars)
for word in targets:
word_freq = Counter(word)
for char in word:
if word_freq[char] > chars_freq[char]:
break
ans += 1
return ans
例如,答案应该是2
,但我得到的是4
。谁能帮忙?谢谢。