Number of Valid Words in a Sentence LeetCode solution in Java

1
class Solution {
2
public int countValidWords(String sentence) {
3
String regex = "^([a-z]+(-?[a-z]+)?)?(!|\\.|,)?$";
4
String r2 = "[^0-9]+";
5
String[] arr = sentence.split("\\s+");
6
int ans = 0;
7
for (String s : arr) {
8
if (s.matches(regex) && s.matches(r2)) {
9
ans++;
10
// System.out.println(s);
11
}
12
}
13
return ans;
14
}
15
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0