LeetCode笔记:318. Maximum Product of Word Lengths

问题:

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

Example 1:

Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]

Return 16

The two words can be “abcw”, “xtfn”.

Example 2:

Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]

Return 4

The two words can be “ab”, “cd”.

Example 3:

Given [“a”, “aa”, “aaa”, “aaaa”]

Return 0

No such pair of words.

大意:

给出一个字符串数组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

没有满足条件的字符串对。

思路:

题目要求找出两个尽可能长的不包含相同字符的字符串并求出其长度乘积。

要保证不包含相同的字符,一种方法是对每个字符串做一次处理,用26位的数组记录每个小写字母出现的次数,对于每两个字符串,看相同的字母是否都出现了,如果都出现了就不考虑,否则就计算一下长度乘积看看比结果大不大。

另一种做法是用一个26位的数字来记录,每一位对应当前字母是否出现过,两个字符串对应的数字相与为0的话就说明没有相同的字母了。

代码(Java):

public class Solution {
    public int maxProduct(String[] words) {
        int res = 0;
        for (int i = 0; i < words.length; i++) {
            int[] one = new int[26];
            for (int k = 0; k < words[i].length(); k++) one[words[i].charAt(k)-'a']++;

            for (int j = i+1; j < words.length; j++) {
                int[] two = new int[26];
                for (int k = 0; k < words[j].length(); k++) two[words[j].charAt(k)-'a']++;

                boolean hasSame = false;
                for (int k = 0; k < 26; k++) {
                    if (one[k] > 0 && two[k] > 0) hasSame = true;
                }
                if (hasSame) continue;
                else {
                    res = res >= words[i].length() * words[j].length() ? res : words[i].length() * words[j].length();
                }
            }
        }
        return res;
    }
}

他山之石:

public class Solution {
    public int maxProduct(String[] words) {
        int max = 0;

        Arrays.sort(words, new Comparator<String>(){
            public int compare(String a, String b){
                return b.length() - a.length();
            }
        });

        int[] masks = new int[words.length]; // alphabet masks

        for(int i = 0; i < masks.length; i++){
            for(char c: words[i].toCharArray()){
                masks[i] |= 1 << (c - 'a');
            }
        }

        for(int i = 0; i < masks.length; i++){
            if(words[i].length() * words[i].length() <= max) break; //prunning
            for(int j = i + 1; j < masks.length; j++){
                if((masks[i] & masks[j]) == 0){
                    max = Math.max(max, words[i].length() * words[j].length());
                    break; //prunning
                }
            }
        }

        return max;
    }
}

这个做法首先使用的26位数字来记录字母出现与否,此外先将字符串按照长度排个序,从最长的开始处理,如果处理到后面,一个字符串长度本身的平方都做不到比结果更大,那就算乘以一个后面比它还短的长度,更不可能会得到更大的值了,就可以直接跳过很多。


合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值