public class Solution {
    public int MaxProduct(string[] words) {
        if (words == null || words.Length == 0)
            {
                return 0;
            }
            int len = words.Length;
            int[] value = new int[len];
            for (int i = 0; i < len; i++)
            {
                string tmp = words[i];
                value[i] = 0;
                for (int j = 0; j < tmp.Length; j++)
                {
                    value[i] |= 1 << (tmp[j] - 'a');
                }
            }
            int maxProduct = 0;
            for (int i = 0; i < len; i++)
            {
                for (int j = i + 1; j < len; j++)
                {
                    if ((value[i] & value[j]) == 0 && (words[i].Length * words[j].Length > maxProduct))
                    {
                        maxProduct = words[i].Length * words[j].Length;
                    }
                }
            }
            return maxProduct;
    }
}

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

posted on 2017-06-08 08:29  Sempron2800+  阅读(114)  评论(0编辑  收藏  举报