318. Maximum Product of Word Lengths
Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
Output: 16
Explanation: The two words can be "abcw", "xtfn".Input: ["a","ab","abc","d","cd","bcd","abcd"]
Output: 4
Explanation: The two words can be "ab", "cd".Input: ["a","aa","aaa","aaaa"]
Output: 0
Explanation: No such pair of words.解题要点:
class Solution {
public int maxProduct(String[] words) {
if(words == null && words.length == 0) return 0;
int n = words.length;
int[] value = new int[n];
int res = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < words[i].length(); j++){
value[i] |= 1 << (words[i].charAt(j) - 'a');
}
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if((value[i] & value[j]) == 0){
res = Math.max(res, words[i].length() * words[j].length());
}
}
}
return res;
}
}Last updated