> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Counting Words With a Given Prefix

> Tested Python solution for LeetCode 2185 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 2185, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [String Matching](/catalog/topics/string-matching). [View on LeetCode](https://leetcode.com/problems/counting-words-with-a-given-prefix/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2185   # by problem number
lcpy gen -s counting_words_with_a_given_prefix   # by problem name
```

## Problem

You are given an array of strings `words` and a string `pref`.

Return the number of strings in `words` that contain `pref` as a prefix.

A prefix of a string `s` is any leading contiguous substring of `s`.

### Examples

```
Input: words = ["pay","attention","practice","attend"], pref = "at"
Output: 2
Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
```

```
Input: words = ["leetcode","win","loops","success"], pref = "code"
Output: 0
Explanation: There are no strings that contain "code" as a prefix.
```

### Constraints

* 1 \<= words.length \<= 100
* 1 \<= words\[i].length, pref.length \<= 100
* words\[i] and pref consist of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/counting_words_with_a_given_prefix/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/counting_words_with_a_given_prefix/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sum(len(w) for w in words) * len(pref)) worst case via startswith
    # Space: O(1)
    def prefix_count(self, words: list[str], pref: str) -> int:
        return sum(1 for word in words if word.startswith(pref))
```

## Complexity

| Time | Space |
| - | - |
| O(sum(len(w) for w in words) \* len(pref)) worst case via startswith | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.