Skip to content

Commit 8dd81c3

Browse files
committed
Runtime: 560 ms (Top 5.11%) | Memory: 54 MB (Top 5.84%)
1 parent d657322 commit 8dd81c3

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
// Runtime: 560 ms (Top 5.11%) | Memory: 54 MB (Top 5.84%)
12
#include <regex>
23

34
class Solution {
45
public:
5-
int countValidWords(string sentence) {
6+
int countValidWords(string sentence) {
67

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;
119

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]*[!,.]?");
2212

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+
};

0 commit comments

Comments
 (0)