Solution URL
https://leetcode.com/submissions/detail/413895423/
代码
class Solution {
public int maxProduct(String[] words) {
//ans: https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/
int n = words.length;
int[] a = new int[n];
//计算出每一个的字符的二进制表示法
for(int i = 0; i < n; i++){
for(int j = 0; j < words[i].length(); j++){
a[i] |= 1 << (words[i].charAt(j) - 'a');
}
}
//与运算,得到不包含相同字母的字符串长度,标记最大长度
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
//没有相同的,则结果为0
if((a[i] & a[j]) == 0){
//计算最大长度
ans = Math.max(ans, words[i].length() * words[j].length());
}
}
}
return ans;
}
}