1935. 可以输入的最大单词数 - 力扣(LeetCode)

可以把这个问题分解成几个简单的步骤:
-
将字符串
text按空格拆分成单词数组。 -
对于每个单词,检查是否包含了
brokenLetters中的任意一个字符。 -
如果不包含这些字符,则说明这个单词可以完整输入,计数加一。
-
最后返回可以输入的单词数量。
下面是一个 Python 实现:
def can_be_typed_words(text: str, brokenLetters: str) -> int:
words = text.split()
broken_set = set(brokenLetters)
count = 0
for word in words:
if not any(char in broken_set for char in word):
count += 1
return count
示例:
text = "hello world example"
brokenLetters = "ad"
# "hello" 和 "world" 都不含 "a" 或 "d",所以可以打出来,"example" 包含 "a",不能打
# 输出:2
print(can_be_typed_words(text, brokenLetters)) # 输出:2
381

被折叠的 条评论
为什么被折叠?



