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

# Shortest Completing Word Python Solution

> Tested Python solution for LeetCode 748 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 748, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/shortest-completing-word/description/).

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

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

## Problem

Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`.

A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than once in `licensePlate`, then it must appear in the word the same number of times or more.

For example, if `licensePlate = "aBc 12c"`, then it contains letters `'a'`, `'b'` (ignoring case), and `'c'` twice. Possible **completing** words are `"abccdef"`, `"caaacab"`, and `"cbca"`.

Return *the shortest **completing** word in* `words`\*. It is guaranteed an answer exists. If there are multiple shortest **completing** words, return the **first** one that occurs in `words`.

### Examples

```
Input: licensePlate = "1s3 PSt", words = ["step","steps","stripe","stepple"]
Output: "steps"
Explanation: licensePlate contains letters 's', 'p', 's' (ignoring case), and 't'.
"step" contains 't' and 'p', but only contains 1 's'.
"steps" contains 't', 'p', and both 's' characters.
"stripe" is missing an 's'.
"stepple" is missing an 's'.
Since "steps" is the only word containing all the letters, that is the answer.
```

```
Input: licensePlate = "1s3 456", words = ["looks","pest","stew","show"]
Output: "pest"
Explanation: licensePlate only contains the letter 's'. All the words contain 's', but among these "pest", "stew", and "show" are shortest. The answer is "pest" because it is the word that appears earliest of the 3.
```

### Constraints

* 1 \<= licensePlate.length \<= 7
* licensePlate contains digits, letters (uppercase or lowercase), or space ' '.
* 1 \<= words.length \<= 1000
* 1 \<= words\[i].length \<= 15
* words\[i] consists of lower case English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n * (m + k)) - n words, m word length, k <= 7 plate letters
    # Space: O(1) - at most 26 letters per counter
    def shortest_completing_word(self, license_plate: str, words: list[str]) -> str:
        need = Counter(c for c in license_plate.lower() if c.isalpha())
        best: str | None = None
        for word in words:
            count = Counter(word)
            if all(count[ch] >= k for ch, k in need.items()) and (
                best is None or len(word) < len(best)
            ):
                best = word
        return best or ""
```

## Complexity

| Time | Space |
| - | - |
| O(n \* (m + k)) - n words, m word length, k \<= 7 plate letters | O(1) - at most 26 letters per counter |

## Tags


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