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 thenumber of words intextyou can fully type using this keyboard.
classSolution {
publicintcanBeTypedWords(String text, String brokenLetters) {
Set<Character> broken =new HashSet<>();
for (char c : brokenLetters.toCharArray()) broken.add(c);
int ans = 0;
for (String word : text.split(" ")) {
boolean ok =true;
for (char c : word.toCharArray()) if (broken.contains(c)) { ok =false; break; }
if (ok) ans++;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
funcanBeTypedWords(text: String, brokenLetters: String): Int {
val broken = brokenLetters.toSet()
var ans = 0for (word in text.split(" ")) {
if (word.all { it!in broken }) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
classSolution:
defcanBeTypedWords(self, text: str, brokenLetters: str) -> int:
broken = set(brokenLetters)
ans =0for word in text.split():
if all(c notin broken for c in word):
ans +=1return ans