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

# Maximum Product of Word Lengths

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

LeetCode 318, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/maximum-product-of-word-lengths/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 318   # by problem number
lcpy gen -s maximum_product_of_word_lengths   # by problem name
```

## Problem

Given a string array \<code>words\</code>, return \<em>the maximum value of\</em> \<code>length(word\[i]) \* length(word\[j])\</code> \<em>where the two words do not share common letters\</em>. If no such two words exist, return \<code>0\</code>.

### Examples

```
Input: words = ["abcw","baz","foo","bar","xtfn","abcdef"]
Output: 16
Explanation: The two words can be "abcw", "xtfn".
```

```
Input: words = ["a","ab","abc","d","cd","bcd","abcd"]
Output: 4
Explanation: The two words can be "ab", "cd".
```

```
Input: words = ["a","aa","aaa","aaaa"]
Output: 0
Explanation: No such pair of words.
```

### Constraints

* 2 \<= words.length \<= 1000
* 1 \<= words\[i].length \<= 1000
* words\[i] consists only of lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * (L + n)) where n = len(words) and L = max word length
    # Space: O(n)
    def max_product(self, words: list[str]) -> int:
        masks = [self._letter_mask(word) for word in words]
        best = 0
        for i, mask_i in enumerate(masks):
            for j in range(i + 1, len(masks)):
                if mask_i & masks[j] == 0:
                    best = max(best, len(words[i]) * len(words[j]))
        return best

    def _letter_mask(self, word: str) -> int:
        mask = 0
        for char in word:
            mask |= 1 << (ord(char) - ord("a"))
        return mask
```

## Complexity

| Time | Space |
| - | - |
| O(n \* (L + n)) where n = len(words) and L = max word length | O(n) |

## Tags


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