File tree 1 file changed +19
-18
lines changed
scripts/algorithms/N/Number of Valid Words in a Sentence
1 file changed +19
-18
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 560 ms (Top 5.11%) | Memory: 54 MB (Top 5.84%)
1
2
#include < regex>
2
3
3
4
class Solution {
4
5
public:
5
- int countValidWords (string sentence) {
6
+ int countValidWords (string sentence) {
6
7
7
- int count = 0 ;
8
-
9
- // Defining the regex pattern
10
- regex valid_word (" [a-z]*([a-z]-[a-z])?[a-z]*[!,.]?" );
8
+ int count = 0 ;
11
9
12
- // splitting the sentence to words
13
- stringstream s (sentence);
14
- string word;
15
- while (getline (s, word, ' ' )) {
16
-
17
- // Checking if the word matches the regex pattern
18
- if (word != " " && regex_match (word, valid_word)){
19
- ++count;
20
- }
21
- }
10
+ // Defining the regex pattern
11
+ regex valid_word (" [a-z]*([a-z]-[a-z])?[a-z]*[!,.]?" );
22
12
23
- return count;
24
- }
25
- };
13
+ // splitting the sentence to words
14
+ stringstream s (sentence);
15
+ string word;
16
+ while (getline (s, word, ' ' )) {
17
+
18
+ // Checking if the word matches the regex pattern
19
+ if (word != " " && regex_match (word, valid_word)){
20
+ ++count;
21
+ }
22
+ }
23
+
24
+ return count;
25
+ }
26
+ };
You can’t perform that action at this time.
0 commit comments