> ## 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.

# Most Common Word Python Solution with Tests

> Tested Python solution for LeetCode 819 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 819, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/most-common-word/description/).

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

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

## Problem

Given a string `paragraph` and a string array of the banned words `banned`, return *the most frequent word that is not banned*. It is **guaranteed** there is **at least one word** that is not banned, and that the answer is **unique**.

The words in `paragraph` are **case-insensitive** and the answer should be returned in **lowercase**.

**Note** that words can not contain punctuation symbols.

### Examples

```
Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation: "hit" occurs 3 times, but it is a banned word. "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
```

```
Input: paragraph = "a.", banned = []
Output: "a"
```

### Constraints

* 1 \<= paragraph.length \<= 1000
* paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".
* 0 \<= banned.length \<= 100
* 1 \<= banned\[i].length \<= 10
* banned\[i] consists of only lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import re


class Solution:
    # Time: O(P + B)
    # Space: O(P)
    def most_common_word(self, paragraph: str, banned: list[str]) -> str:
        banned_set = set(banned)
        counts: dict[str, int] = {}
        for word in re.findall(r"[a-z]+", paragraph.lower()):
            if word not in banned_set:
                counts[word] = counts.get(word, 0) + 1
        return max(counts, key=lambda w: counts[w])
```

## Complexity

| Time | Space |
| - | - |
| O(P + B) | O(P) |

## Tags


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