I split the text into words and check each one to see if it contains any broken letters. If a word contains a broken letter, it cannot be typed, so I skip it. Otherwise, I count it as typeable. To make the check fast, I put all broken letters into a set for O(1) membership tests.
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
broken = set(brokenLetters)
count = 0
for word in text.split():
if all(ch not in broken for ch in word):
count += 1
return count- Time: O(n)
- Space: O(b) where b is number of broken letter (26 max)