From 037a3098e940b9e18bca295bc9fae47d6c09acd4 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Fri, 3 Apr 2026 08:16:12 +0800 Subject: [PATCH] test: add leetcode 2185 with tests, complexity docs, and migration - Migrate CountingWordsWithAGivenPrefix2185 to proper package structure - Add comprehensive javadoc with detailed O(n*m) time and O(1) space complexity analysis - Add test suite with LeetCode example test cases - Update project documentation and statistics --- README.md | 4 +- docs/EASY.md | 3 +- src/CountingWordsWithAGivenPrefix2185.java | 18 -------- .../CountingWordsWithAGivenPrefix2185.java | 37 +++++++++++++++++ ...CountingWordsWithAGivenPrefix2185Test.java | 41 +++++++++++++++++++ 5 files changed, 82 insertions(+), 21 deletions(-) delete mode 100644 src/CountingWordsWithAGivenPrefix2185.java create mode 100644 src/main/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185.java create mode 100644 src/test/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185Test.java diff --git a/README.md b/README.md index b5bffd7..eab1020 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 52 problems solved (migrated to new structure) +📊 **Statistics**: 53 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| -| Easy | 36 | +| Easy | 37 | | Medium | 15 | | Hard | 1 | diff --git a/docs/EASY.md b/docs/EASY.md index f72c5e2..772b7b6 100644 --- a/docs/EASY.md +++ b/docs/EASY.md @@ -1,6 +1,6 @@ # Easy Problems -Total: 36 problems solved +Total: 37 problems solved ## Solutions @@ -37,6 +37,7 @@ Total: 36 problems solved | 2011 | [Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/) | [FinalValueOfVariableAfterPerformingOperations2011.java](../src/main/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011.java) | [FinalValueOfVariableAfterPerformingOperations2011Test.java](../src/test/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011Test.java) | | 2032 | [Two Out of Three](https://leetcode.com/problems/two-out-of-three/) | [TwoOutOfThree2032.java](../src/main/java/com/leetcode/easy/TwoOutOfThree2032.java) | [TwoOutOfThree2032Test.java](../src/test/java/com/leetcode/easy/TwoOutOfThree2032Test.java) | | 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/) | [CheckIfAllAsAppearsBeforeAllBs2124.java](../src/main/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124.java) | [CheckIfAllAsAppearsBeforeAllBs2124Test.java](../src/test/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124Test.java) | +| 2185 | [Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | [CountingWordsWithAGivenPrefix2185.java](../src/main/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185.java) | [CountingWordsWithAGivenPrefix2185Test.java](../src/test/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185Test.java) | | 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks/) | [CountAsterisks2315.java](../src/main/java/com/leetcode/easy/CountAsterisks2315.java) | [CountAsterisks2315Test.java](../src/test/java/com/leetcode/easy/CountAsterisks2315Test.java) | | 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [ConvertTheTemperature2469.java](../src/main/java/com/leetcode/easy/ConvertTheTemperature2469.java) | [ConvertTheTemperature2469Test.java](../src/test/java/com/leetcode/easy/ConvertTheTemperature2469Test.java) | | 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target/) | [NumberOfEmployeesWhoMetTheTarget2798.java](../src/main/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798.java) | [NumberOfEmployeesWhoMetTheTarget2798Test.java](../src/test/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798Test.java) | diff --git a/src/CountingWordsWithAGivenPrefix2185.java b/src/CountingWordsWithAGivenPrefix2185.java deleted file mode 100644 index 17fac5e..0000000 --- a/src/CountingWordsWithAGivenPrefix2185.java +++ /dev/null @@ -1,18 +0,0 @@ -public class CountingWordsWithAGivenPrefix2185 { - public int prefixCount(String[] words, String pref) { - int res = 0; - for (String word : words) { - if (word.startsWith(pref)) { - res += 1; - } - } - return res; - } - - public static void main(String[] args) { - String[] words = new String[]{"pay", "attention", "practice", "attend"}; - String pref = "at"; - // 2 - System.out.println(new CountingWordsWithAGivenPrefix2185().prefixCount(words, pref)); - } -} diff --git a/src/main/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185.java b/src/main/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185.java new file mode 100644 index 0000000..305c2bf --- /dev/null +++ b/src/main/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185.java @@ -0,0 +1,37 @@ +// Tags: Array, String +package com.leetcode.easy; + +public class CountingWordsWithAGivenPrefix2185 { + /** + * Returns the number of strings in the array that have the given prefix. + * + *

This method iterates through each word in the array and checks if it starts + * with the specified prefix using String.startsWith(). + * + * @param words the array of strings to search through + * @param pref the prefix string to match + * @return the count of words that start with the given prefix + * + *

Time Complexity: O(n * m) + *

+ * + *

Space Complexity: O(1) + *

+ */ + public int prefixCount(String[] words, String pref) { + int res = 0; + for (String word : words) { + if (word.startsWith(pref)) { + res += 1; + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185Test.java b/src/test/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185Test.java new file mode 100644 index 0000000..93632de --- /dev/null +++ b/src/test/java/com/leetcode/easy/CountingWordsWithAGivenPrefix2185Test.java @@ -0,0 +1,41 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CountingWordsWithAGivenPrefix2185Test { + private CountingWordsWithAGivenPrefix2185 solution; + + @BeforeEach + void setUp() { + solution = new CountingWordsWithAGivenPrefix2185(); + } + + @Test + void testPrefixCount_Example1_TwoMatches() { + // Input: words = ["pay","attention","practice","attend"], pref = "at" + // Output: 2 + // Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend". + String[] words = {"pay", "attention", "practice", "attend"}; + String pref = "at"; + + int result = solution.prefixCount(words, pref); + + assertEquals(2, result); + } + + @Test + void testPrefixCount_Example2_NoMatches() { + // Input: words = ["leetcode","win","loops","success"], pref = "code" + // Output: 0 + // Explanation: There are no strings that contain "code" as a prefix. + String[] words = {"leetcode", "win", "loops", "success"}; + String pref = "code"; + + int result = solution.prefixCount(words, pref); + + assertEquals(0, result); + } +}