refactor: CountWords (#5428)

* refactor: CountWords

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-08-28 22:34:46 +02:00 committed by GitHub
parent a23e9b0ba8
commit e2aaefebd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 33 deletions

View File

@ -8,17 +8,26 @@ public final class 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 * @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) { public static int wordCount(String s) {
if (s == null || s.isEmpty()) { if (s == null || s.isEmpty()) {
return 0; 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) { private static String removeSpecialCharacters(String s) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) { for (char c : s.toCharArray()) {
@ -30,12 +39,12 @@ public final class CountWords {
} }
/** /**
* counts the number of words in a sentence but ignores all potential * Counts the number of words in a sentence, ignoring all non-alphanumeric characters that do
* non-alphanumeric characters that do not represent a word. runs in O(n) * not contribute to word formation. This method has a time complexity of O(n), where n is the
* where n is the length of s * length of the input string.
* *
* @param s String: sentence with word(s) * @param s the input string
* @return int: number of words * @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) { public static int secondaryWordCount(String s) {
if (s == null) { if (s == null) {

View File

@ -2,36 +2,30 @@ package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashMap; import java.util.stream.Stream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class CountWordsTest { 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()) { @ParameterizedTest
assertEquals(CountWords.wordCount(tc.getKey()), tc.getValue()); @MethodSource("wordCountTestCases")
} void testWordCount(String input, int expectedCount) {
assertEquals(expectedCount, CountWords.wordCount(input));
} }
@Test @ParameterizedTest
public void testSecondaryWordCount() { @MethodSource("secondaryWordCountTestCases")
HashMap<String, Integer> testCases = new HashMap<>(); void testSecondaryWordCount(String input, int expectedCount) {
testCases.put("", 0); assertEquals(expectedCount, CountWords.secondaryWordCount(input));
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);
for (final var tc : testCases.entrySet()) { private static Stream<Arguments> wordCountTestCases() {
assertEquals(CountWords.secondaryWordCount(tc.getKey()), tc.getValue()); 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));
} }
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));
} }
} }