# Maximum length product of unique words in JavaScript

JavascriptWeb DevelopmentFront End Technology

## Problem

We are required to write a JavaScript function that takes in an array of strings (only lowercase string alphabets) as the first and the only argument.

The function should pick two such strings from the array that shares no common characters and have the maximum product of their lengths. And then our function should return the length product of two such strings. If there exist no such strings in the array, we should return 0.

For example, if the input to the function is −

const arr = ["karl", "n", "the", "car", "mint", "alpha"];

Then the output should be −

const output = 20;

## Output Explanation:

The words ‘mint’ and ‘alpha’ share no common word and the product of their lengths is 20.

## Example

The code for this will be −

const arr = ["karl", "n", "the", "car", "mint", "alpha"];
const maxLengthProduct = (arr = []) => {
const array = [];
arr.forEach(str => {
let curr = 0;
for(let i = 0; i < str.length; i++){
curr |= 1<<(str.charCodeAt(i) - 97);
};
array.push(curr);
});
let res = 0;
for(let i = 0 ; i < array.length; i++) {
for(let j = i + 1; j < array.length ; j++) {
if((array[i] & array[j]) === 0) {
res = Math.max(res, arr[i].length * arr[j].length);
}
}
}
return res;
};
console.log(maxLengthProduct(arr));

## Output

And the output in the console will be −

20
Published on 20-Mar-2021 05:38:34