File tree Expand file tree Collapse file tree 4 files changed +86
-55
lines changed
solution/1900-1999/1935.Maximum Number of Words You Can Type Expand file tree Collapse file tree 4 files changed +86
-55
lines changed Original file line number Diff line number Diff line change @@ -116,12 +116,14 @@ class Solution {
116
116
public:
117
117
int canBeTypedWords(string text, string brokenLetters) {
118
118
bool s[ 26] {};
119
- for (char& c : brokenLetters) {
119
+ for (char c : brokenLetters) {
120
120
s[ c - 'a'] = true;
121
121
}
122
122
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) {
125
127
if (s[ c - 'a'] ) {
126
128
--ans;
127
129
break;
@@ -131,21 +133,6 @@ public:
131
133
}
132
134
return ans;
133
135
}
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
- }
149
136
};
150
137
```
151
138
@@ -217,6 +204,31 @@ impl Solution {
217
204
}
218
205
```
219
206
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
+
220
232
<!-- tabs: end -->
221
233
222
234
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -117,12 +117,14 @@ class Solution {
117
117
public:
118
118
int canBeTypedWords(string text, string brokenLetters) {
119
119
bool s[ 26] {};
120
- for (char& c : brokenLetters) {
120
+ for (char c : brokenLetters) {
121
121
s[ c - 'a'] = true;
122
122
}
123
123
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) {
126
128
if (s[ c - 'a'] ) {
127
129
--ans;
128
130
break;
@@ -132,21 +134,6 @@ public:
132
134
}
133
135
return ans;
134
136
}
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
- }
150
137
};
151
138
```
152
139
@@ -218,6 +205,31 @@ impl Solution {
218
205
}
219
206
```
220
207
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
+
221
233
<!-- tabs: end -->
222
234
223
235
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -2,12 +2,14 @@ class Solution {
2
2
public:
3
3
int canBeTypedWords (string text, string brokenLetters) {
4
4
bool s[26 ]{};
5
- for (char & c : brokenLetters) {
5
+ for (char c : brokenLetters) {
6
6
s[c - ' a' ] = true ;
7
7
}
8
8
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) {
11
13
if (s[c - ' a' ]) {
12
14
--ans;
13
15
break ;
@@ -17,19 +19,4 @@ class Solution {
17
19
}
18
20
return ans;
19
21
}
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
+ };
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments