File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 232
232
303|[ Range Sum Query - Immutable] ( ./0303-range-sum-query-immutable.js ) |Easy|
233
233
306|[ Additive Number] ( ./0306-additive-number.js ) |Medium|
234
234
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|
235
236
322|[ Coin Change] ( ./0322-coin-change.js ) |Medium|
236
237
326|[ Power of Three] ( ./0326-power-of-three.js ) |Easy|
237
238
328|[ Odd Even Linked List] ( ./0328-odd-even-linked-list.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments