Skip to content

Commit 34bd5b5

Browse files
add 2047
1 parent 793a36e commit 34bd5b5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2047|[Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2047.java) ||Easy||
1112
|2044|[Count Number of Maximum Bitwise-OR Subsets](https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2044.java) ||Medium||
1213
|2043|[Simple Bank System](https://leetcode.com/problems/simple-bank-system/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2043.java) ||Medium||
1314
|2042|[Check if Numbers Are Ascending in a Sentence](https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2042.java) ||Easy||
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2047 {
4+
public static class Solution1 {
5+
public int countValidWords(String sentence) {
6+
String[] tokens = sentence.split("\\s+");
7+
int count = 0;
8+
for (String token : tokens) {
9+
int hyphenCount = 0;
10+
int punctuationMarkCount = 0;
11+
boolean valid = true;
12+
if (token.isEmpty() || token.equals("") || token.length() == 0) {
13+
continue;
14+
}
15+
for (int i = 0; i < token.length(); i++) {
16+
if (token.charAt(i) == '-') {
17+
hyphenCount++;
18+
if (hyphenCount > 1 || i == 0 || i == token.length() - 1 || !Character.isAlphabetic(token.charAt(i - 1)) || !Character.isAlphabetic(token.charAt(i + 1))) {
19+
valid = false;
20+
break;
21+
}
22+
} else if (token.charAt(i) == '!' || token.charAt(i) == '.' || token.charAt(i) == ',') {
23+
punctuationMarkCount++;
24+
if (punctuationMarkCount > 1 || i != token.length() - 1) {
25+
valid = false;
26+
break;
27+
}
28+
} else if (Character.isDigit(token.charAt(i))) {
29+
valid = false;
30+
break;
31+
} else if (Character.isDigit(token.charAt(i))) {
32+
valid = false;
33+
break;
34+
}
35+
}
36+
if (valid) {
37+
count++;
38+
}
39+
}
40+
return count;
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)