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
Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐