原题链接:
https://leetcode.cn/problems/maximum-product-of-word-lengths/description/

解法1: 简单位运算模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public int maxProduct(String[] words) {
HashMap<Integer,Integer> hash = new HashMap<>();
for(var ss : words){
int t = 0;
for(int i = 0 ; i < ss.length() ; i++){
int u = ss.charAt(i) - 'a';
t |= (1 << u);
}

int l = hash.getOrDefault(t,0);
hash.put(t,Math.max(l,ss.length()));
}

int ans = 0;

for (var a : hash.keySet()){
for (var b: hash.keySet()){
if((a & b) == 0){
ans = Math.max(ans,hash.get(a) * hash.get(b));
}
}
}

return ans;
}
}