[LeetCode] Maximum Number of Words You Can Type

1935. Maximum Number of Words You Can Type

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class Solution {
public:
int canBeTypedWords(string text, string brokenLetters) {
istringstream iss(text);
vector<string> filter;
string token;
while (getline(iss, token, ' ')) if (!token.empty()) filter.push_back(token);

for(auto& ch : brokenLetters) {
vector<string> nfilter;
for(auto& f : filter) if(!count(f.begin(), f.end(), ch)) nfilter.push_back(f);
swap(filter, nfilter);
}

return filter.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/09/15/PS/LeetCode/maximum-number-of-words-you-can-type/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.