classSolution { public: intcanBeTypedWords(string text, string brokenLetters){ unordered_set<char> cannot(brokenLetters.begin(), brokenLetters.end()); int ans = 0; bool can = true; for (char c : text) { if (c == ' ') { ans += can; can = true; continue; } if (cannot.count(c)) { can = false; } } return ans + can; } };
Python
1 2 3 4 5 6 7 8 9 10 11 12 13
''' Author: LetMeFly Date: 2025-09-15 21:48:47 LastEditors: LetMeFly.xyz LastEditTime: 2025-09-15 21:56:54 ''' classSolution: defcanBeTypedWords(self, text: str, brokenLetters: str) -> int: ans = 0 cannot = set(brokenLetters) for word in text.split(): ans += all(c notin cannot for c in word) return ans
Python
1 2 3 4 5 6 7 8 9 10
''' Author: LetMeFly Date: 2025-09-15 21:58:43 LastEditors: LetMeFly.xyz LastEditTime: 2025-09-15 21:58:57 ''' classSolution: defcanBeTypedWords(self, text: str, brokenLetters: str) -> int: cannot = set(brokenLetters) returnsum(all(c notin cannot for c in word) for word in text.split())
funccanBeTypedWords(text string, brokenLetters string) (ans int) { cannot := map[byte]struct{}{} for i := range brokenLetters { cannot[brokenLetters[i]] = struct{}{} } can := true for i := range text { if text[i] == ' ' { if can { ans++ } else { can = true } continue } if _, in := cannot[text[i]]; in { can = false } } if can { ans++ } return }