Skip to content

Commit e2aaefe

Browse files
alxkmalxkm
and
alxkm
authored
refactor: CountWords (#5428)
* refactor: CountWords * checkstyle: fix formatting --------- Co-authored-by: alxkm <[email protected]>
1 parent a23e9b0 commit e2aaefe

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

src/main/java/com/thealgorithms/others/CountWords.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ private CountWords() {
88
}
99

1010
/**
11-
* @brief counts the number of words in the input string
11+
* Counts the number of words in the input string. Words are defined as sequences of
12+
* characters separated by whitespace.
13+
*
1214
* @param s the input string
13-
* @return the number of words in the input string
15+
* @return the number of words in the input string, or 0 if the string is null or empty
1416
*/
1517
public static int wordCount(String s) {
1618
if (s == null || s.isEmpty()) {
1719
return 0;
1820
}
19-
return s.trim().split("[\\s]+").length;
21+
return s.trim().split("\\s+").length;
2022
}
2123

24+
/**
25+
* Removes all special characters from the input string, leaving only alphanumeric characters
26+
* and whitespace.
27+
*
28+
* @param s the input string
29+
* @return a string containing only alphanumeric characters and whitespace
30+
*/
2231
private static String removeSpecialCharacters(String s) {
2332
StringBuilder sb = new StringBuilder();
2433
for (char c : s.toCharArray()) {
@@ -30,12 +39,12 @@ private static String removeSpecialCharacters(String s) {
3039
}
3140

3241
/**
33-
* counts the number of words in a sentence but ignores all potential
34-
* non-alphanumeric characters that do not represent a word. runs in O(n)
35-
* where n is the length of s
42+
* Counts the number of words in a sentence, ignoring all non-alphanumeric characters that do
43+
* not contribute to word formation. This method has a time complexity of O(n), where n is the
44+
* length of the input string.
3645
*
37-
* @param s String: sentence with word(s)
38-
* @return int: number of words
46+
* @param s the input string
47+
* @return the number of words in the input string, with special characters removed, or 0 if the string is null
3948
*/
4049
public static int secondaryWordCount(String s) {
4150
if (s == null) {

src/test/java/com/thealgorithms/others/CountWordsTest.java

+19-25
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,30 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import java.util.HashMap;
6-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
79

810
class CountWordsTest {
9-
@Test
10-
public void testWordCount() {
11-
HashMap<String, Integer> testCases = new HashMap<>();
12-
testCases.put("", 0);
13-
testCases.put(null, 0);
14-
testCases.put("aaaa bbb cccc", 3);
15-
testCases.put("note extra spaces here", 4);
16-
testCases.put(" a b c d e ", 5);
1711

18-
for (final var tc : testCases.entrySet()) {
19-
assertEquals(CountWords.wordCount(tc.getKey()), tc.getValue());
20-
}
12+
@ParameterizedTest
13+
@MethodSource("wordCountTestCases")
14+
void testWordCount(String input, int expectedCount) {
15+
assertEquals(expectedCount, CountWords.wordCount(input));
2116
}
2217

23-
@Test
24-
public void testSecondaryWordCount() {
25-
HashMap<String, Integer> testCases = new HashMap<>();
26-
testCases.put("", 0);
27-
testCases.put(null, 0);
28-
testCases.put("aaaa bbb cccc", 3);
29-
testCases.put("this-is-one-word!", 1);
30-
testCases.put("What, about, this? Hmmm----strange", 4);
31-
testCases.put("word1 word-2 word-3- w?o,r.d.@!@#$&*()<>4", 4);
18+
@ParameterizedTest
19+
@MethodSource("secondaryWordCountTestCases")
20+
void testSecondaryWordCount(String input, int expectedCount) {
21+
assertEquals(expectedCount, CountWords.secondaryWordCount(input));
22+
}
23+
24+
private static Stream<Arguments> wordCountTestCases() {
25+
return Stream.of(Arguments.of("", 0), Arguments.of(null, 0), Arguments.of("aaaa bbb cccc", 3), Arguments.of("note extra spaces here", 4), Arguments.of(" a b c d e ", 5));
26+
}
3227

33-
for (final var tc : testCases.entrySet()) {
34-
assertEquals(CountWords.secondaryWordCount(tc.getKey()), tc.getValue());
35-
}
28+
private static Stream<Arguments> secondaryWordCountTestCases() {
29+
return Stream.of(Arguments.of("", 0), Arguments.of(null, 0), Arguments.of("aaaa bbb cccc", 3), Arguments.of("this-is-one-word!", 1), Arguments.of("What, about, this? Hmmm----strange", 4), Arguments.of("word1 word-2 word-3- w?o,r.d.@!@#$&*()<>4", 4));
3630
}
3731
}

0 commit comments

Comments
 (0)