Skip to content

Commit f5a825a

Browse files
committed
2023-01-07 update: added "2047. Number of Valid Words in a Sentence"
1 parent 109ea89 commit f5a825a

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
355355
| 2032. Two Out of Three | [Link](https://leetcode.com/problems/two-out-of-three/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/TwoOutOfThree.java) |
356356
| 2037. Minimum Number of Moves to Seat Everyone | [Link](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MinimumNumberOfMovesToSeatEveryone.java) |
357357
| 2042. Check if Numbers Are Ascending in a Sentence | [Link](https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/CheckIfNumbersAreAscendingInASentence.java) |
358+
| 2047. Number of Valid Words in a Sentence | [Link](https://leetcode.com/problems/number-of-valid-words-in-a-sentence/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/NumberOfValidWordsInASentence.java) |
358359
| 2053. Kth Distinct String in an Array | [Link](https://leetcode.com/problems/kth-distinct-string-in-an-array/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/KthDistinctStringInAnArray.java) |
359360
| 2057. Smallest Index With Equal Value | [Link](https://leetcode.com/problems/smallest-index-with-equal-value/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/SmallestIndexWithEqualValue.java) |
360361
| 2062. Count Vowel Substrings of a String | [Link](https://leetcode.com/problems/count-vowel-substrings-of-a-string/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/CountVowelSubstringsOfAString.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
// https://leetcode.com/problems/number-of-valid-words-in-a-sentence/
4+
public class NumberOfValidWordsInASentence {
5+
6+
private final String input;
7+
8+
public NumberOfValidWordsInASentence(String input) {
9+
this.input = input;
10+
}
11+
12+
public int solution() {
13+
int result = 0;
14+
for (String word : input.split("\\s+")) {
15+
if (word.isEmpty()) {
16+
continue;
17+
}
18+
if (checkFirstRule(word) && checkSecondRule(word) && checkThirdRule(word)) {
19+
result++;
20+
}
21+
}
22+
return result;
23+
}
24+
25+
private boolean checkFirstRule(String token) {
26+
for (int i = 0; i < token.length(); i++) {
27+
char curr = token.charAt(i);
28+
if (Character.isDigit(curr)
29+
|| (Character.isLetter(curr) && Character.isUpperCase(curr))) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
36+
private boolean checkSecondRule(String token) {
37+
int n = token.length();
38+
int count = 0;
39+
for (int i = 0; i < n; i++) {
40+
if (token.charAt(i) == '-') {
41+
count++;
42+
if (count > 1) {
43+
break;
44+
}
45+
if (i == 0 || i == n - 1) {
46+
return false;
47+
}
48+
if (!Character.isLetter(token.charAt(i - 1))
49+
|| !Character.isLetter(token.charAt(i + 1))) {
50+
return false;
51+
}
52+
}
53+
}
54+
return count < 2;
55+
}
56+
57+
private boolean checkThirdRule(String token) {
58+
for (int i = 0; i < token.length() - 1; i++) {
59+
char curr = token.charAt(i);
60+
if (!Character.isLetter(curr)
61+
&& !Character.isDigit(curr) && curr != '-') {
62+
return false;
63+
}
64+
}
65+
return true;
66+
67+
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class NumberOfValidWordsInASentenceTest {
8+
9+
@Test
10+
public void defaultTests() {
11+
assertEquals(3, new NumberOfValidWordsInASentence("cat and dog").solution());
12+
assertEquals(0, new NumberOfValidWordsInASentence("!this 1-s b8d!").solution());
13+
assertEquals(
14+
5,
15+
new NumberOfValidWordsInASentence(
16+
"alice and bob are playing stone-game10"
17+
).solution()
18+
);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)