318 Maximum Product of Word Lengths 最大单词长度乘积

给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
示例 1:
输入 ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
返回 16
两个单词可以为 "abcw", "xtfn"。
示例 2:
输入 ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
返回 4
两个单词可以为 "ab", "cd"。
示例 3:
输入 ["a", "aa", "aaa", "aaaa"]
返回 0
没有这样的两个单词。

详见:https://leetcode.com/problems/maximum-product-of-word-lengths/description/

class Solution {
public:
    int maxProduct(vector<string>& words) {
        int res=0;
        vector<int> mask(words.size(),0);
        for(int i=0;i<words.size();++i)
        {
            for(char c:words[i])
            {
                mask[i]|=1<<(c-'a');
            }
            for(int j=0;j<i;++j)
            {
                if(!(mask[i]&mask[j]))
                {
                    res=max(res,int(words[i].size()*words[j].size()));
                }
            }
        }
        return res;
    }
};

 参考:http://www.cnblogs.com/grandyang/p/5090058.html

原文地址:https://www.cnblogs.com/xidian2014/p/8832159.html