Skip to content

Commit 49c587e

Browse files
committed
maximum product of word length js πŸŽ‰
1 parent a34a0f2 commit 49c587e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

β€Žmaximum-product-of-word-lengths.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {number}
4+
*/
5+
const maxProduct = words => {
6+
const n = words.length;
7+
const values = Array(n).fill(0);
8+
9+
for (let i = 0; i < n; i++) {
10+
for (let ch of words[i]) {
11+
values[i] |= 1 << (ch.charCodeAt(0) - 'a'.charCodeAt(0));
12+
}
13+
}
14+
15+
let max = 0;
16+
17+
for (let i = 0; i < n; i++) {
18+
for (let j = i + 1; j < n; j++) {
19+
if ((values[i] & values[j]) === 0 && words[i].length * words[j].length > max) {
20+
max = words[i].length * words[j].length;
21+
}
22+
}
23+
}
24+
25+
return max;
26+
};
27+

0 commit comments

Comments
Β (0)