Java实现 LeetCode 318 最大单词长度乘积

318. 最大单词长度乘积

给定一个字符串数组 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
解释: 不存在这样的两个单词。

PS:
全是小写字母, 可以用一个32为整数表示一个word中出现的字母,
hash[i]存放第i个单词出现过的字母, a对应32位整数的最后一位,
b对应整数的倒数第二位, 依次类推. 时间复杂度O(N^2)
判断两两单词按位与的结果, 如果结果为0且长度积大于最大积则更新

class Solution {
   public int maxProduct(String[] words) {
        int n = words.length;
        int[] wordsBit = new int[n];
        for (int i = 0; i < n; i++) {
            wordsBit[i] = convertWordToBit(words[i]);
        }
        int max = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if ((wordsBit[i] & wordsBit[j]) == 0) {
                    int newLength = words[i].length() * words[j].length();
                    max = max < newLength ? newLength : max;
                }
            }
        }
        return max;
    }

    private int convertWordToBit(String word) {
        int wordBit = 0;
        int n = word.length();
        for (int i = 0; i < n; i++) {
            wordBit = wordBit | (1 << (word.charAt(i) - 'a'));
        }
        return wordBit;
    }
}
发布了1434 篇原创文章 · 获赞 1万+ · 访问量 158万+

猜你喜欢

转载自blog.csdn.net/a1439775520/article/details/104698217