Skip to content

Commit c55f16d

Browse files
committed
add maximum-product-of-word-lengths script.
1 parent 846bd63 commit c55f16d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {number}
4+
*/
5+
const maxProduct = function(words) {
6+
if (words == null || words.length === 0) return 0;
7+
let len = words.length;
8+
let value = [];
9+
for (let i = 0; i < len; i++) {
10+
let tmp = words[i];
11+
value[i] = 0;
12+
for (let j = 0; j < tmp.length; j++) {
13+
value[i] |= 1 << (tmp.charAt(j).charCodeAt(0) - "a".charCodeAt(0));
14+
}
15+
}
16+
let maxProductNum = 0;
17+
for (let i = 0; i < len; i++)
18+
for (let j = i + 1; j < len; j++) {
19+
if (
20+
(value[i] & value[j]) === 0 &&
21+
words[i].length * words[j].length > maxProductNum
22+
)
23+
maxProductNum = words[i].length * words[j].length;
24+
}
25+
return maxProductNum;
26+
};

0 commit comments

Comments
 (0)