forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-product-of-word-lengths.js
More file actions
46 lines (38 loc) · 1.5 KB
/
maximum-product-of-word-lengths.js
File metadata and controls
46 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Given a string array words, return the maximum value of
// length(word[i]) * length(word[j]) where the two words
// do not share common letters. If no such two words exist, return 0.
// Constraints:
// 2 <= words.length <= 1000
// 1 <= words[i].length <= 1000
// words[i] consists only of lowercase English letters.
const findMaxProduct = (words) => {
// We can use a 32-bit integer to create a unique mask for every string
// where each bit will correspond to a certain letter
// For example: 'abcw' --> 00000000010000000000000000000111
// w cba
const masks = {};
let maxProduct = 0;
const start = 'a'.codePointAt(0);
// For every word
for (let i = 0; i < words.length; i++) {
let mask = 0;
// we create a mask
for (const c of words[i]) {
mask |= 1 << (c.codePointAt(0) - start);
}
// We compare a new mask with other masks
for (const k of Object.keys(masks)) {
// And if they don't have common letters
if (!(masks[k] & mask)) {
// we calculate the product of their lengths and compare it with
// the max product we found earlier
maxProduct = Math.max(maxProduct, words[i].length * words[k].length);
}
}
masks[i] = mask;
}
return maxProduct;
};
console.log(findMaxProduct(['abcw', 'baz', 'foo', 'bar', 'xtfn', 'abcdef'])); // 16
console.log(findMaxProduct(['a', 'ab', 'abc', 'd', 'cd', 'bcd', 'abcd'])); // 4
console.log(findMaxProduct(['a', 'aa', 'aaa', 'aaaa'])); // 0