Skip to content

Commit 58634a4

Browse files
authored
feat: add solutions to lc problem: No.1935 (#4722)
1 parent c29de98 commit 58634a4

File tree

4 files changed

+86
-55
lines changed

4 files changed

+86
-55
lines changed

solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ class Solution {
116116
public:
117117
int canBeTypedWords(string text, string brokenLetters) {
118118
bool s[26]{};
119-
for (char& c : brokenLetters) {
119+
for (char c : brokenLetters) {
120120
s[c - 'a'] = true;
121121
}
122122
int ans = 0;
123-
for (auto& w : split(text, ' ')) {
124-
for (char& c : w) {
123+
stringstream ss(text);
124+
string w;
125+
while (ss >> w) {
126+
for (char c : w) {
125127
if (s[c - 'a']) {
126128
--ans;
127129
break;
@@ -131,21 +133,6 @@ public:
131133
}
132134
return ans;
133135
}
134-
135-
vector<string> split(const string& s, char c) {
136-
vector<string> ans;
137-
string t;
138-
for (char d : s) {
139-
if (d == c) {
140-
ans.push_back(t);
141-
t.clear();
142-
} else {
143-
t.push_back(d);
144-
}
145-
}
146-
ans.push_back(t);
147-
return ans;
148-
}
149136
};
150137
```
151138
@@ -217,6 +204,31 @@ impl Solution {
217204
}
218205
```
219206

207+
#### C#
208+
209+
```cs
210+
public class Solution {
211+
public int CanBeTypedWords(string text, string brokenLetters) {
212+
bool[] s = new bool[26];
213+
foreach (char c in brokenLetters) {
214+
s[c - 'a'] = true;
215+
}
216+
int ans = 0;
217+
string[] words = text.Split(' ');
218+
foreach (string w in words) {
219+
foreach (char c in w) {
220+
if (s[c - 'a']) {
221+
--ans;
222+
break;
223+
}
224+
}
225+
++ans;
226+
}
227+
return ans;
228+
}
229+
}
230+
```
231+
220232
<!-- tabs:end -->
221233

222234
<!-- solution:end -->

solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ class Solution {
117117
public:
118118
int canBeTypedWords(string text, string brokenLetters) {
119119
bool s[26]{};
120-
for (char& c : brokenLetters) {
120+
for (char c : brokenLetters) {
121121
s[c - 'a'] = true;
122122
}
123123
int ans = 0;
124-
for (auto& w : split(text, ' ')) {
125-
for (char& c : w) {
124+
stringstream ss(text);
125+
string w;
126+
while (ss >> w) {
127+
for (char c : w) {
126128
if (s[c - 'a']) {
127129
--ans;
128130
break;
@@ -132,21 +134,6 @@ public:
132134
}
133135
return ans;
134136
}
135-
136-
vector<string> split(const string& s, char c) {
137-
vector<string> ans;
138-
string t;
139-
for (char d : s) {
140-
if (d == c) {
141-
ans.push_back(t);
142-
t.clear();
143-
} else {
144-
t.push_back(d);
145-
}
146-
}
147-
ans.push_back(t);
148-
return ans;
149-
}
150137
};
151138
```
152139
@@ -218,6 +205,31 @@ impl Solution {
218205
}
219206
```
220207

208+
#### C#
209+
210+
```cs
211+
public class Solution {
212+
public int CanBeTypedWords(string text, string brokenLetters) {
213+
bool[] s = new bool[26];
214+
foreach (char c in brokenLetters) {
215+
s[c - 'a'] = true;
216+
}
217+
int ans = 0;
218+
string[] words = text.Split(' ');
219+
foreach (string w in words) {
220+
foreach (char c in w) {
221+
if (s[c - 'a']) {
222+
--ans;
223+
break;
224+
}
225+
}
226+
++ans;
227+
}
228+
return ans;
229+
}
230+
}
231+
```
232+
221233
<!-- tabs:end -->
222234

223235
<!-- solution:end -->

solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ class Solution {
22
public:
33
int canBeTypedWords(string text, string brokenLetters) {
44
bool s[26]{};
5-
for (char& c : brokenLetters) {
5+
for (char c : brokenLetters) {
66
s[c - 'a'] = true;
77
}
88
int ans = 0;
9-
for (auto& w : split(text, ' ')) {
10-
for (char& c : w) {
9+
stringstream ss(text);
10+
string w;
11+
while (ss >> w) {
12+
for (char c : w) {
1113
if (s[c - 'a']) {
1214
--ans;
1315
break;
@@ -17,19 +19,4 @@ class Solution {
1719
}
1820
return ans;
1921
}
20-
21-
vector<string> split(const string& s, char c) {
22-
vector<string> ans;
23-
string t;
24-
for (char d : s) {
25-
if (d == c) {
26-
ans.push_back(t);
27-
t.clear();
28-
} else {
29-
t.push_back(d);
30-
}
31-
}
32-
ans.push_back(t);
33-
return ans;
34-
}
35-
};
22+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int CanBeTypedWords(string text, string brokenLetters) {
3+
bool[] s = new bool[26];
4+
foreach (char c in brokenLetters) {
5+
s[c - 'a'] = true;
6+
}
7+
int ans = 0;
8+
string[] words = text.Split(' ');
9+
foreach (string w in words) {
10+
foreach (char c in w) {
11+
if (s[c - 'a']) {
12+
--ans;
13+
break;
14+
}
15+
}
16+
++ans;
17+
}
18+
return ans;
19+
}
20+
}

0 commit comments

Comments
 (0)