Skip to content

Commit d5291f8

Browse files
solves number of valid words in a sentence
1 parent 829c711 commit d5291f8

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@
487487
| 2032 | [Two Out of Three](https://leetcode.com/problems/two-out-of-three) | [![Java](assets/java.png)](src/TwoOutOfThree.java) | |
488488
| 2037 | [Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone) | [![Java](assets/java.png)](src/MinimumNumberOfMovesToSeatEveryone.java) | |
489489
| 2042 | [Check if Numbers Are Ascending in a Sentence](https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence) | [![Java](assets/java.png)](src/CheckIfNumbersAreAscendingInASentence.java) | |
490-
| 2047 | [Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence) | | |
490+
| 2047 | [Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence) | [![Java](assets/java.png)](src/NumberOfValidWordsInASentence.java) | |
491491
| 2053 | [Kth Distinct String in an Array](https://leetcode.com/problems/kth-distinct-string-in-an-array) | | |
492492
| 2057 | [Smallest Index With Equal Value](https://leetcode.com/problems/smallest-index-with-equal-value) | | |
493493
| 2062 | [Count Vowel Substrings of a String](https://leetcode.com/problems/count-vowel-substrings-of-a-string) | | |
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// https://leetcode.com/problems/number-of-valid-words-in-a-sentence
2+
// T: O(|sentence|)
3+
// S: O(|sentence|)
4+
5+
import java.util.Set;
6+
7+
public class NumberOfValidWordsInASentence {
8+
private static final Set<Character> PUNCTUATIONS = Set.of('!', '.', ',');
9+
10+
public int countValidWords(String sentence) {
11+
StringBuilder word = new StringBuilder();
12+
int validWords= 0;
13+
for (int i = 0 ; i < sentence.length() ; i++) {
14+
if (sentence.charAt(i) == ' ') {
15+
validWords += isValidWord(word) ? 1 : 0;
16+
word = new StringBuilder();
17+
} else word.append(sentence.charAt(i));
18+
}
19+
return validWords + (isValidWord(word) ? 1 : 0);
20+
}
21+
22+
private boolean isValidWord(StringBuilder s) {
23+
if (s.length() == 0) return false;
24+
for (int i = 0, hyphens = 0 ; i < s.length() ; i++) {
25+
if (isDigit(s.charAt(i))) return false;
26+
else if (isHyphen(s.charAt(i))) {
27+
if (hyphens > 0) return false;
28+
hyphens++;
29+
if (i == 0 || i == s.length() - 1 || !isAlphabet(s.charAt(i + 1))) return false;
30+
} else if (isPunctuationMark(s.charAt(i)) && i < s.length() - 1) return false;
31+
}
32+
return true;
33+
}
34+
35+
private boolean isDigit(char c) {
36+
return Character.isDigit(c);
37+
}
38+
39+
private boolean isHyphen(char c) {
40+
return c == '-';
41+
}
42+
43+
private boolean isAlphabet(char c) {
44+
return Character.isAlphabetic(c);
45+
}
46+
47+
private boolean isPunctuationMark(char c) {
48+
return PUNCTUATIONS.contains(c);
49+
}
50+
}

0 commit comments

Comments
 (0)