/** * @param {string[]}words * @return {number} */ var maxProduct = function (words) { let max = 0; words = words .map((word) => { return { set: newSet(word.split("")), word }; }) .sort((a, b) => b.set.size - a.set.size);
const check = (set1, set2) => { const [maxSet, minSet] = [set1, set2].sort( (a, b) => b.set.size - a.set.size ); return [...minSet.set].every((char) => !maxSet.set.has(char)); }; for (let i = 0; i < words.length; i++) { const set1 = words[i]; for (let j = i + 1; j < words.length; j++) { const set2 = words[j]; if (check(set1, set2)) max = Math.max(max, set1.word.length * set2.word.length); } } return max; };