-
Notifications
You must be signed in to change notification settings - Fork 16
/
NumberOfValidWordsInASentence.java
50 lines (42 loc) · 1.57 KB
/
NumberOfValidWordsInASentence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://leetcode.com/problems/number-of-valid-words-in-a-sentence
// T: O(|sentence|)
// S: O(|sentence|)
import java.util.Set;
public class NumberOfValidWordsInASentence {
private static final Set<Character> PUNCTUATIONS = Set.of('!', '.', ',');
public int countValidWords(String sentence) {
StringBuilder word = new StringBuilder();
int validWords= 0;
for (int i = 0 ; i < sentence.length() ; i++) {
if (sentence.charAt(i) == ' ') {
validWords += isValidWord(word) ? 1 : 0;
word = new StringBuilder();
} else word.append(sentence.charAt(i));
}
return validWords + (isValidWord(word) ? 1 : 0);
}
private boolean isValidWord(StringBuilder s) {
if (s.length() == 0) return false;
for (int i = 0, hyphens = 0 ; i < s.length() ; i++) {
if (isDigit(s.charAt(i))) return false;
else if (isHyphen(s.charAt(i))) {
if (hyphens > 0) return false;
hyphens++;
if (i == 0 || i == s.length() - 1 || !isAlphabet(s.charAt(i + 1))) return false;
} else if (isPunctuationMark(s.charAt(i)) && i < s.length() - 1) return false;
}
return true;
}
private boolean isDigit(char c) {
return Character.isDigit(c);
}
private boolean isHyphen(char c) {
return c == '-';
}
private boolean isAlphabet(char c) {
return Character.isAlphabetic(c);
}
private boolean isPunctuationMark(char c) {
return PUNCTUATIONS.contains(c);
}
}