318. 最大单词长度乘积.
难度中等
给你一个字符串数组 words
,找出并返回 length(words[i]) * length(words[j])
的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0
。
示例 1:
输入:words =["abcw","baz","foo","bar","xtfn","abcdef"]
输出:16 解释
:这两个单词为 "abcw", "xtfn"
。
示例 2:
输入:words =["a","ab","abc","d","cd","bcd","abcd"]
输出:4 解释
:这两个单词为"ab", "cd"
。
示例 3:
输入:words =["a","aa","aaa","aaaa"]
输出:0 解释
:不存在这样的两个单词。
class Solution { public: int maxProduct(vector<string>& words) { int n = words.size(); vector<int>mask(n,0); for(int i = 0; i < n;i++) { for(char cc: words[i]) { mask[i] |= (1<<(cc-'a')); } } int res = 0; for(int i = 0; i < n;i++) { for(int j = i+1; j< n;j++) { if ((mask[i]& mask[j])==0) { res = max(res, int(words[i].size())*int(words[j].size())); } } } return res; } };