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
// @Title: 单词长度的最大乘积 (单词长度的最大乘积)
// @Author: 15816537946@163.com
// @Date: 2022-03-16 22:40:40
// @Runtime: 12 ms
// @Memory: 6.3 MB
func maxProduct(words []string) int {
	rec := make([]int, len(words))
	for i, w := range words {
		for _, c := range w {
			rec[i] |= 1 << (c - 'a')
		}
	}

	var ans int
	for i := range rec {
		for j := i + 1; j < len(rec); j++ {
			if rec[i]&rec[j] == 0 {
				ans = max(ans, len(words[i])*len(words[j]))
			}
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}