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

# Word Subsets Python Solution with Tests

> Tested Python solution for LeetCode 916 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 916, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/word-subsets/description/).

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

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

## Problem

You are given two string arrays \<code>words1\</code> and \<code>words2\</code>.\</p>

\<p>A string \<code>b\</code> is a \<strong>subset\</strong> of string \<code>a\</code> if every letter in \<code>b\</code> occurs in \<code>a\</code> including multiplicity.\</p>

\<ul>
\<li>For example, \<code>"wrr"\</code> is a subset of \<code>"warrior"\</code> but is not a subset of \<code>"world"\</code>.\</li>
\</ul>

\<p>A string \<code>a\</code> from \<code>words1\</code> is \<strong>universal\</strong> if for every string \<code>b\</code> in \<code>words2\</code>, \<code>b\</code> is a subset of \<code>a\</code>.\</p>

\<p>Return an array of all the \<strong>universal\</strong> strings in \<code>words1\</code>. You may return the answer in \<strong>any order\</strong>.\</p>

### Examples

```
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]
```

```
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","o"]
Output: ["google","leetcode"]
```

### Constraints

* 1 \<= words1.length, words2.length \<= 10^4
* 1 \<= words1\[i].length, words2\[i].length \<= 10
* words1\[i] and words2\[i] consist only of lowercase English letters.
* All the strings of words1 are unique.

## Solution

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

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


class Solution:
    # Time: O(w1 * avg_len + w2 * avg_len)
    # Space: O(1)
    def word_subsets(self, words1: list[str], words2: list[str]) -> list[str]:
        need = Counter()
        for word in words2:
            counts = Counter(word)
            for letter, count in counts.items():
                need[letter] = max(need[letter], count)
        return [word for word in words1 if not (need - Counter(word))]
```

## Complexity

| Time | Space |
| - | - |
| O(w1 \* avg\_len + w2 \* avg\_len) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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