Note:
先处理一遍数组,把所有的字符串变成位运算表示,这样两个进行一下与运算,如果等于0就说明没有重复的字符
之后在枚举处理后的字符串,计算一下最大的长度即可
代码如下:
class Solution {
public:
int maxProduct(vector<string>& words) {
int ans = 0;
vector<int> words2int;
for(auto word: words){
int mask = 0;
for(auto c: word){
int x = c - 'a';
mask |= 1 << x;
}
words2int.push_back(mask);
}
for(int i = 0; i < words2int.size(); i ++)
for(int j = 0; j < words2int.size(); j ++)
if((words2int[i] & words2int[j]) == 0){
int res = words[i].size() * words[j].size();
ans = max(res, ans);
}
return ans;
}
};