# 32ms Java AC solution

• ``````public class Solution {
public int maxProduct(String[] words) {
int max = 0;

Arrays.sort(words, new Comparator<String>(){
public int compare(String a, String b){
return b.length() - a.length();
}
});

for(int i = 0; i < masks.length; i++){
for(char c: words[i].toCharArray()){
masks[i] |= 1 << (c - 'a');
}
}

for(int i = 0; i < masks.length; i++){
if(words[i].length() * words[i].length() <= max) break; //prunning
for(int j = i + 1; j < masks.length; j++){
max = Math.max(max, words[i].length() * words[j].length());
break; //prunning
}
}
}

return max;
}
}``````

• How would this work on the following words: "abcde", "fg","hijklmnop"
seems that you'll calculate "abcde" * "fg" and then break before calculating the correct answer.
Or perhaps I'm not fully understanding your solution.

1. The array is sorted as {"hijklmnop", "abcde", "fg"}.
2. The "break" only breaks out the inner "for" loop

• yes, I realized this later. Thanks for responding.

• I think there is no need to sort

• I think it is necessary for pruning.

• Since the overall complexity is O(n^2), sorting does not matter too much. Sorting will enable early pruning.

• i'm a little bit confused about that “if(words[i].length() * words[i].length() <= max) break; //prunning” ？why if the square of words[i].length smaller than max then break?

• That's what sorting is for. So that you can exit if squaring is not larger than the max. Because if you keep going, the product will not get larger.

• And I just tested it without sorting and it's 19 ms. So the nlog(n) could be significant I suppose...

• This post is deleted!

• A slight optimization is moving the second for loop into the first one, so the first loop can end early with pruning before calculating the masks for words with small lengths

• Can someone explain how he is sure that if the AND of two masked number is Zero, then those are always different (no common characters)?

• yes, i also test it in Python, got 78ms without sorting, while 78ms with sorting... but it is worth trying~

• my python version:

``````def maxProduct(self, words):
"""
:type words: List[str]
:rtype: int
"""
n = len(words)
def my_cmp(a,b):
return len(b)-len(a)
sorted_words = sorted(words, my_cmp)#seem not save time :(

count = [0]*n
begin_ord = ord('a')
for i, w in enumerate(sorted_words):
for c in w:
count[i] += 1
max_len = 0
for i in range(n):
if count[i]**2<max_len:
continue
for j in range(i+1, n):

multiple = count[i]*count[j]
if multiple>max_len:
max_len = multiple
break
return max_len``````

• Came to the same solution without sort and pruning. The only benefit of sorting the input array is pruning. But it's hard to prove that the gain of pruning will cover the cost of sorting since it takes O(nlogn) time.

• try to simplify it with Java 8:

``````    Arrays.sort(words, (a, b) -> b.length() - a.length());

for (int i = 0, len = words.length; i < len; i++) {
for (int j = 0; j < words[i].length(); j++) {
masks[i] |= 1 << (words[i].charAt(j) - 'a');
}
}

int max = 0;
for (int i = 0, len = words.length; i < len; i++) {
if (words[i].length() == 0) {
continue;
}
for (int j = i + 1, minLen = max / words[i].length(); j < len && words[j].length() > minLen; j++) {
if ((masks[i] & masks[j]) == 0 && words[i].length() * words[j].length() > max) {
max = words[i].length() * words[j].length();
break;
}
}
}

return max;``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.