Skip to content

Commit 7da1a53

Browse files
committed
Add solution #318
1 parent c4a41eb commit 7da1a53

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
303|[Range Sum Query - Immutable](./0303-range-sum-query-immutable.js)|Easy|
233233
306|[Additive Number](./0306-additive-number.js)|Medium|
234234
316|[Remove Duplicate Letters](./0316-remove-duplicate-letters.js)|Medium|
235+
318|[Maximum Product of Word Lengths](./0318-maximum-product-of-word-lengths.js)|Medium|
235236
322|[Coin Change](./0322-coin-change.js)|Medium|
236237
326|[Power of Three](./0326-power-of-three.js)|Easy|
237238
328|[Odd Even Linked List](./0328-odd-even-linked-list.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 318. Maximum Product of Word Lengths
3+
* https://leetcode.com/problems/maximum-product-of-word-lengths/
4+
* Difficulty: Medium
5+
*
6+
* Given a string array words, return the maximum value of length(word[i]) * length(word[j])
7+
* where the two words do not share common letters. If no such two words exist, return 0.
8+
*/
9+
10+
/**
11+
* @param {string[]} words
12+
* @return {number}
13+
*/
14+
var maxProduct = function(words) {
15+
const letters = words.map(word => Array.from(new Set(word)));
16+
let result = 0;
17+
18+
for (let i = 0; i < words.length - 1; i++) {
19+
for (let j = i + 1; j < words.length; j++) {
20+
if (!letters[i].some(item => letters[j].includes(item))) {
21+
const product = words[i].length * words[j].length;
22+
if (product > result) {
23+
result = product;
24+
}
25+
}
26+
}
27+
}
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)