-
Notifications
You must be signed in to change notification settings - Fork 0
/
318.最大单词长度乘积.js
51 lines (43 loc) · 926 Bytes
/
318.最大单词长度乘积.js
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
47
48
49
50
51
/*
* @Author: xuhuan
* @Date: 2019-08-21 22:08:29
* @LastEditors: xuhuan
* @LastEditTime: 2019-08-21 22:08:29
* @Description:
*/
/**
* @param {string[]} words
* @return {number}
*/
var maxProduct = function(words) {
const length = words.length;
if (length < 2) {
return 0;
}
let res = 0;
const arr = words.map(i => {
let obj = {
length: i.length
}
for (let index = 0; index < i.length; index++) {
const element = i[index];
obj[element] = true;
}
return obj;
})
for (let i = 0; i < length-1; i++) {
for (let j = i + 1; j < length; j++) {
let flag = true;
for (let k = 0; k < arr[j].length; k++) {
if (arr[i][words[j][k]]) {
flag = false;
}
}
if (flag) {
res = Math.max(res, arr[i].length * arr[j].length);
}
}
}
return res;
};
console.log(maxProduct(['abc', 'def']));