# 318. Maximum Product of Word Lengths

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/maximum-product-of-word-lengths/

> 内容描述

```
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

Example 1:

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

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

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

## 解题方案

> 思路 1
******- 时间复杂度: O(N^2)******- 空间复杂度: O(N^2)******


```
The soultion is calcuated by doing a product of the length of
each string to every other string. Anyhow the constraint given is
that the two strings should not have any common character. This
is taken care by creating a unique number for every string. Image
a an 32 bit integer where 0 bit corresponds to 'a', 1st bit
corresponds to 'b' and so on.

Thus if two strings contain the same character when we do and
"AND" the result will not be zero and we can ignore that case.
```

beats 88.8%

```python
class Solution:
    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        lookup = {}
        for word in words:
            mask = 0
            for c in set(word):
                mask |= 1 << (ord(c) - ord('a'))
            lookup[mask] = max(lookup.get(mask, 0), len(word))
        return max([lookup[x] * lookup[y] for x in lookup for y in lookup if not x & y] or [0])
```































































