https://leetcode.com/problems/maximum-product-of-word-lengths/?tab=Description
找出两个没有重复字符的字符串长度乘积最大的值
位运算O(N2)即可
public class Solution {
public int maxProduct(String[] words) {
int len = words.length;
int[] val = new int[len];
for (int j = 0; j < len; j++) {
String word = words[j];
for (int i = 0; i < word.length(); i++) {
val[j] |= (1 << (word.charAt(i) - 'a'));
}
}
int res = 0;
for (int i = 0; i < words.length; i++) {
for (int j = i + 1; j < words.length; j++) {
int cnt1 = val[i];
int cnt2 = val[j];
if ((cnt1 & cnt2) == 0) {
res = Math.max(res, words[i].length() * words[j].length());
}
}
}
return res;
}
}
转载请注明原文地址: https://ju.6miu.com/read-3329.html