123456789101112131415161718192021222324252627 |
- type pair struct {
- _1 int
- _2 int
- }
- func maxProduct(words []string) (product int) {
- n := len(words)
- if n <= 1 {
- return
- }
- pairs := make([]pair, n)
- for i := 0; i < n; i++ {
- mask, l := 0, len(words[i])
- for j := 0; j < l; j++ {
- mask |= 1 << uint(words[i][j]-'a')
- }
- pairs[i] = pair{mask, l}
- }
- for i := 0; i < n-1; i++ {
- for j := i + 1; j < n; j++ {
- if pairs[i]._1&pairs[j]._1 == 0 && product < pairs[i]._2*pairs[j]._2 {
- product = pairs[i]._2 * pairs[j]._2
- }
- }
- }
- return
- }
|