Skip to content

Commit 4f631af

Browse files
committed
leetcode
1 parent 3ece75f commit 4f631af

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

maximum-product-of-word-lengths.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,19 @@
44
*/
55
var maxProduct = function (words) {
66
var max = 0;
7-
var bitDict = [];
7+
var mask = [];
88
for (var i = 0; i < words.length; i++) {
99
for (var j = 0; j < words[i].length; j++) {
10-
bitDict[i] |= Math.pow(2, words[i][j].charCodeAt() - 'a'.charCodeAt());
10+
mask[i] |= Math.pow(2, words[i][j].charCodeAt() - 'a'.charCodeAt());
1111
}
1212
}
1313

1414
for (i = 0; i < words.length; i++) {
15-
for (j = 0; j < words.length; j++) {
16-
if (i !== j && !hasSharedLetter(i, j)) {
15+
for (j = 0; j < i; j++) {
16+
if ((mask[i] & mask[j]) === 0) {
1717
max = Math.max(max, words[i].length * words[j].length);
1818
}
1919
}
2020
}
2121
return max;
22-
23-
function hasSharedLetter(index1, index2) {
24-
if ((bitDict[index1] & bitDict[index2]) === 0) return false;
25-
return true;
26-
}
2722
};

0 commit comments

Comments
 (0)