Leetcode 1935. Maximum Number of Words You Can Type
·
Problem
There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
Given a string text of words separated by a single space (no leading or trailing spaces) and a string brokenLetters of all distinct letter keys that are broken, return the number of words in text you can fully type using this keyboard.
Algorithm
Convert brokenLetters into a set broken for fast character existence checking. Iterate through each character of every word and check if any character exists in the broken set. If found, mark the word as untypeable. Count the typeable words.
Code
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
broken = set(brokenLetters)
cnt = 0
for w in text.split():
flag = 0
for ch in w:
if ch in broken:
flag = 1
break
cnt += not flag
return cnt
更多推荐



所有评论(0)