---
# System prepended metadata

title: 0318. Maximum Product of Word Lengths
tags: [Leetcode, Bit Manipulation, Medium]

---

# 0318. Maximum Product of Word Lengths
###### tags: `Leetcode` `Medium` `Bit Manipulation`
Link: https://leetcode.com/problems/maximum-product-of-word-lengths/
## 思路 $O(N^2+L)$ $O(N)$
$N$ is a number of words and $L$ is a total length of all words together
先用bit mask表示每个word都有哪些字母
然后再比较找出max Product
## Code
```java=
class Solution {
    public int maxProduct(String[] words) {
        int max = 0;
        int n = words.length;
        int[] masks = new int[n];
        for(int i=0; i<n; i++){
            int mask = 0;
            for(char c:words[i].toCharArray()){
                mask |= 1<<(c-'a');
            }
            masks[i] = mask;
        }
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                if((masks[i]&masks[j])==0){
                    max = Math.max(max, words[i].length()*words[j].length());
                }
            }
        }
        return max;
    }
}
```