318. 最大单词长度乘积


2019-02-14 22:38:13 by Sikoay with 0 comments 1 Hits
Blog 1

暴力破解一时爽,一直暴力一直爽....

问题:

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

 

解答:

class Solution {
    public int maxProduct(String[] words) {
        int max_length = 0;
        for (int i = 0; i < words.length ;i++ ) {
            next:for (int n = i+1; n < words.length;n++ ) {
                for(int m = 0;m < words[i].length(); m++){
                    for(int x = 0;x < words[n].length();x++){
                        if(words[i].charAt(m) == words[n].charAt(x)){
                            continue next;
                        }
                    }
                }
                max_length = Math.max(words[i].length() * words[n].length(),max_length);
            }
        }
        return max_length;
    }
}

PS:附上大佬的解题和思路,来看看:

class Solution {
    public int maxProduct(String[] words) {
        /**
        全是小写字母, 可以用一个32为整数表示一个word中出现的字母, 
        hash[i]存放第i个单词出现过的字母, a对应32位整数的最后一位,
        b对应整数的倒数第二位, 依次类推. 时间复杂度O(N^2)
        判断两两单词按位与的结果, 如果结果为0且长度积大于最大积则更新
        **/
        int n = words.length;
        int[] hash = new int[n];
        int max = 0;
        for(int i = 0; i < n; ++i) {
            for(char c : words[i].toCharArray())
                hash[i] |= 1 << (c-'a');
        }
        
        for(int i = 0; i < n-1; ++i) {
            for(int j = i+1; j < n; ++j) {
                if((hash[i] & hash[j]) == 0)
                    max = Math.max(words[i].length() * words[j].length(), max);
            }
        }
        return max;
    }
}
Tags:

回复 (0)

Leave a Comment