Maximum Number of Words You Can Type LeetCode solution in C++

1
class Solution {
2
public:
3
int canBeTypedWords(string text, string brokenLetters) {
4
vector<int> ch(26, 0);
5
// store the broken letters
6
for (char c : brokenLetters) {
7
ch[c - 'a'] = 1;
8
}
9
// breaking text into word using string stream
10
stringstream s(text); // Used for breaking words
11
string word; // to store individual words
12

13
int count = 0, flag = 0;
14
while (s >> word) {
15
flag = 0;
16
for (char &c : word) {
17
// if the word contains a broken letter mark flag=1
18
if (ch[c - 'a'] == 1) {
19
flag = 1;
20
break;
21
}
22
}
23
// if flag is 0 which means that there is no broken letter in word so
24
// increase the count
25
if (flag == 0) count++;
26
}
27
return count;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0