Skip to content
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
8 changes: 0 additions & 8 deletions String/2185. Counting Words With a Given Prefix.py

This file was deleted.

Empty file.
28 changes: 28 additions & 0 deletions problems/easy/counting_words_with_a_given_prefix_2185/solution.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
Loading