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)