From a892d39474f1992492ecd7179ddf07a4a39ad3f0 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Fri, 20 Mar 2026 07:48:24 +0800 Subject: [PATCH] Refactor LeetCode 2185: Counting Words With a Given Prefix to new directory structure - Migrate solution from String/ directory to problems/easy/counting_words_with_a_given_prefix_2185/ - Add comprehensive docstring with Args, Returns, and complexity analysis - Rename method from prefixCount to prefix_count (snake_case) - Add pytest-based test cases --- ...185. Counting Words With a Given Prefix.py | 8 ------ .../__init__.py | 0 .../solution.py | 28 +++++++++++++++++++ .../test_solution.py | 14 ++++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) delete mode 100644 String/2185. Counting Words With a Given Prefix.py create mode 100644 problems/easy/counting_words_with_a_given_prefix_2185/__init__.py create mode 100644 problems/easy/counting_words_with_a_given_prefix_2185/solution.py create mode 100644 problems/easy/counting_words_with_a_given_prefix_2185/test_solution.py diff --git a/String/2185. Counting Words With a Given Prefix.py b/String/2185. Counting Words With a Given Prefix.py deleted file mode 100644 index 5d73736..0000000 --- a/String/2185. Counting Words With a Given Prefix.py +++ /dev/null @@ -1,8 +0,0 @@ -# String, Array -class Solution: - def prefixCount(self, words: List[str], pref: str) -> int: - res = 0 - for word in words: - if word.startswith(pref): - res += 1 - return res diff --git a/problems/easy/counting_words_with_a_given_prefix_2185/__init__.py b/problems/easy/counting_words_with_a_given_prefix_2185/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/easy/counting_words_with_a_given_prefix_2185/solution.py b/problems/easy/counting_words_with_a_given_prefix_2185/solution.py new file mode 100644 index 0000000..623c31e --- /dev/null +++ b/problems/easy/counting_words_with_a_given_prefix_2185/solution.py @@ -0,0 +1,28 @@ +# Tags: Array, String +from typing import List + + +class Solution: + def prefix_count(self, words: List[str], pref: str) -> int: + """ + Count how many words in the list start with the given prefix. + + Args: + words: List of words to check + pref: The prefix string to search for + + Returns: + The number of words that start with the given prefix + + Time complexity: O(n × m) where n is the number of words and m is the prefix length + - Each word's startswith() check: O(m) in the worst case + - Checking all n words: O(n × m) + + Space complexity: O(1) auxiliary space + - Only uses a counter variable 'res' + """ + res = 0 + for word in words: + if word.startswith(pref): + res += 1 + return res diff --git a/problems/easy/counting_words_with_a_given_prefix_2185/test_solution.py b/problems/easy/counting_words_with_a_given_prefix_2185/test_solution.py new file mode 100644 index 0000000..09cb43c --- /dev/null +++ b/problems/easy/counting_words_with_a_given_prefix_2185/test_solution.py @@ -0,0 +1,14 @@ +import pytest +from tests.base_test import BaseTestSolution +from .solution import Solution + + +class TestSolution(BaseTestSolution): + solution = Solution() + + @pytest.mark.parametrize("method_name, words, pref, expected, timeout", [ + ('prefix_count', ["pay", "attention", "practice", "attend"], "at", 2, None), + ('prefix_count', ["leetcode", "win", "loops", "success"], "code", 0, None), + ]) + def test_prefix_count(self, method_name, words, pref, expected, timeout): + self._run_test(self.solution, method_name, (words, pref,), expected, timeout)