1160. 拼写单词

题目

利用列表 remove 方法,检查 chars 中是否有足够的字母拼写 word

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def countCharacters(self, words, chars: str) -> int:

words_ = ''
for w in words:
lchars = list(chars)
try:
for l in w:
lchars.remove(l)
except:
continue
words_ += w

return len(words_)
# 152 ms 14.1 MB