Skip to content

refactor: CountWords #5428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/main/java/com/thealgorithms/others/CountWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ private CountWords() {
}

/**
* @brief counts the number of words in the input string
* Counts the number of words in the input string. Words are defined as sequences of
* characters separated by whitespace.
*
* @param s the input string
* @return the number of words in the input string
* @return the number of words in the input string, or 0 if the string is null or empty
*/
public static int wordCount(String s) {
if (s == null || s.isEmpty()) {
return 0;
}
return s.trim().split("[\\s]+").length;
return s.trim().split("\\s+").length;
}

/**
* Removes all special characters from the input string, leaving only alphanumeric characters
* and whitespace.
*
* @param s the input string
* @return a string containing only alphanumeric characters and whitespace
*/
private static String removeSpecialCharacters(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
Expand All @@ -30,12 +39,12 @@ private static String removeSpecialCharacters(String s) {
}

/**
* counts the number of words in a sentence but ignores all potential
* non-alphanumeric characters that do not represent a word. runs in O(n)
* where n is the length of s
* Counts the number of words in a sentence, ignoring all non-alphanumeric characters that do
* not contribute to word formation. This method has a time complexity of O(n), where n is the
* length of the input string.
*
* @param s String: sentence with word(s)
* @return int: number of words
* @param s the input string
* @return the number of words in the input string, with special characters removed, or 0 if the string is null
*/
public static int secondaryWordCount(String s) {
if (s == null) {
Expand Down
44 changes: 19 additions & 25 deletions src/test/java/com/thealgorithms/others/CountWordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@

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

import java.util.HashMap;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class CountWordsTest {
@Test
public void testWordCount() {
HashMap<String, Integer> testCases = new HashMap<>();
testCases.put("", 0);
testCases.put(null, 0);
testCases.put("aaaa bbb cccc", 3);
testCases.put("note extra spaces here", 4);
testCases.put(" a b c d e ", 5);

for (final var tc : testCases.entrySet()) {
assertEquals(CountWords.wordCount(tc.getKey()), tc.getValue());
}
@ParameterizedTest
@MethodSource("wordCountTestCases")
void testWordCount(String input, int expectedCount) {
assertEquals(expectedCount, CountWords.wordCount(input));
}

@Test
public void testSecondaryWordCount() {
HashMap<String, Integer> testCases = new HashMap<>();
testCases.put("", 0);
testCases.put(null, 0);
testCases.put("aaaa bbb cccc", 3);
testCases.put("this-is-one-word!", 1);
testCases.put("What, about, this? Hmmm----strange", 4);
testCases.put("word1 word-2 word-3- w?o,r.d.@!@#$&*()<>4", 4);
@ParameterizedTest
@MethodSource("secondaryWordCountTestCases")
void testSecondaryWordCount(String input, int expectedCount) {
assertEquals(expectedCount, CountWords.secondaryWordCount(input));
}

private static Stream<Arguments> wordCountTestCases() {
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));
}

for (final var tc : testCases.entrySet()) {
assertEquals(CountWords.secondaryWordCount(tc.getKey()), tc.getValue());
}
private static Stream<Arguments> secondaryWordCountTestCases() {
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));
}
}