Skip to content

Latest commit

 

History

History
170 lines (133 loc) · 4.54 KB

File metadata and controls

170 lines (133 loc) · 4.54 KB

题目描述

给定一个字符串数组 words,请计算当两个字符串 words[i]words[j] 不包含相同字符时,它们长度的乘积的最大值。假设字符串中只包含英语的小写字母。如果没有不包含相同字符的一对字符串,返回 0。

 

示例 1:

输入: words = ["abcw","baz","foo","bar","fxyz","abcdef"]
输出: 16 
解释: 这两个单词为 "abcw", "fxyz"。它们不包含相同字符,且长度的乘积最大。

示例 2:

输入: words = ["a","ab","abc","d","cd","bcd","abcd"]
输出: 4 
解释: 这两个单词为 "ab", "cd"

示例 3:

输入: words = ["a","aa","aaa","aaaa"]
输出: 0 
解释: 不存在这样的两个单词。

 

提示:

  • 2 <= words.length <= 1000
  • 1 <= words[i].length <= 1000
  • words[i] 仅包含小写字母

 

注意:本题与主站 318 题相同:https://leetcode-cn.com/problems/maximum-product-of-word-lengths/

解法

因为只有 26 个小写字符,所以可以用一个 int32 存储字符的出现情况,然后枚举最大乘积

Python3

class Solution:
    def maxProduct(self, words: List[str]) -> int:
        n = len(words)
        mask = [0 for _ in range(n)]
        for i, word in enumerate(words):
            for ch in word:
                mask[i] |= 1 << (ord(ch) - ord('a'))
        ans = 0
        for i in range(0, n - 1):
            for j in range(i + 1, n):
                if mask[i] & mask[j] == 0:
                    ans = max(ans, len(words[i]) * len(words[j]))
        return ans

Java

class Solution {
    public int maxProduct(String[] words) {
        int n = words.length;
        int[] mask = new int[n];
        for (int i = 0; i < n; i++) {
            for (char ch : words[i].toCharArray()) {
                mask[i] |= 1 << (ch - 'a');
            }
        }
        int ans = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if ((mask[i] & mask[j]) == 0) {
                    ans = Math.max(ans, words[i].length() * words[j].length());
                }
            }
        }
        return ans;
    }
}

Go

func maxProduct(words []string) int {
	n := len(words)
	mask := make([]int32, n)
	for i, word := range words {
		for _, r := range word {
			mask[i] |= 1 << (r - 'a')
		}
	}
	ans := 0
	for i := 0; i < n-1; i++ {
		for j := i + 1; j < n; j++ {
			if mask[i]&mask[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
}

C++

class Solution {
public:
    int maxProduct(vector<string>& words) {
        vector<vector<bool>> hash(words.size(), vector<bool>(26, false));
        for (int i = 0; i < words.size(); i++)
            for (char c: words[i])
                hash[i][c - 'a'] = true;

        int res = 0;
        for (int i = 0; i < words.size(); i++) {
            for (int j = i + 1; j < words.size(); j++) {
                int k = 0;
                for (; k < 26; k++) {
                    if (hash[i][k] && hash[j][k])
                        break;
                }

                if (k == 26) {
                    int prod = words[i].size() * words[j].size();
                    res = max(res, prod);
                }
            }
        }

        return res;
    }
};

...