`

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.

给定一个字符串数组,从中找出两个字符串使它们的乘积最大,但是这两个字符串中不存在相同的字符。如果直接查找,数组中每两个字符串都要相互比较,字符串中的字符也要比较,时间复杂度太大。我们可以采用位运算,先将字符串中出现的字符用二进制位来表示,一共有26个字符,我们用一个32位的整数就可以表示。然后判断两个字符串中是否存在相同的字符时,可以将两个字符串的对应的二进制数进行位与运算,如果为0说明不存在相同字符,如果不为0说明存在相同字符。时间复杂度为O(n^2)。代码如下:
public class Solution {
    public int maxProduct(String[] words) {
        if(words == null || words.length == 0) return 0;
        int max = 0;
        int[] bitString = new int[words.length];
        for(int i = 0; i < words.length; i++) {
            char[] w = words[i].toCharArray();
            for(int j = 0; j < w.length; j++) {
                bitString[i] |= 1 << (w[j] - 'a');
            }
        }
        
        for(int i = 0; i < bitString.length - 1; i++) {
            for(int j = i + 1; j < bitString.length; j++) {
                if((bitString[i] & bitString[j]) == 0)
                    max = Math.max(max, words[i].length() * words[j].length());
            }
        }
        return max;
    }
}

0
1
分享到:
评论

相关推荐

    Coding Interview In Java

    232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 Permutation Sequence 573 237 Generate Parentheses 575 238 Combination Sum 577 239 Combination...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    Product of Word Lengths 【这个题目LTE 复杂度已经降下了】 最长不重复字符串3. Longest Substring Without Repeating Characters ----2016.10.08 移动0到末尾 283. Move Zeroes 翻转二叉树 226. Invert Binary ...

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    816 and 32 Bit LFSR with Maximum Length Feedback Polynomial using VHDL.pdf

    As it is simple counter so it can count maximum of 2n -1 by using maximum feedback polynomial. Here in this paper we implemented 8, 16 and 32-bit LFSR on FPGA by using VHDL to study the performance ...

    poj 1003 Hangover

    and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/...

    Direction-Change Features of Imaginary Strokes

    We found suitable direction-change features of the imaginary strokes in the pen-up state for on-line handwritten cursive character recognition. Our method simultaneously uses both directional features...

    MLE_MAP_Part1_zip_MLE_lengths3o_

    《MLE_MAP_Part1_zip_MLE_lengths3o_》是一个与最大似然估计(MLE, Maximum Likelihood Estimation)相关的学习资料,由斯坦福大学提供的部分课程内容组成。在这个压缩包中,主要包含了一份名为“MLE_MAP_Part1.pdf...

    leetcode答案-leetcode:leetcode

    leetcode ...product of word lengths 一开始想到了排序,然后从大到小进行遍历,但是仍然超时,后来在每次判断是否有相同字符之前,先判断是否乘积大于目前的最大值,可以AC,目前看来,排序并没有什么

    Design of a freeform, dual fields-of-view, dual focal lengths, off-axis three-mirror imaging system with a point-by-point construction-iteration process

    In this Letter, we propose a novel configuration and design method of freeform, dual fields-of-view (FOVs), dual focal lengths, off-axis three-mirror zoom imaging systems. The switch of the two zooms ...

    Scrum.and.Xp.from.the.Trenches.2nd.Edition.1329224272

    Under the leadership of Henrik Kniberg they experimented with different team sizes, different sprint lengths, different ways of defining "done", different formats for product backlogs and sprint ...

    Variation of Lasing Wavelength of Fiber Grating Semiconductor Laser with Temperature for Different External Cavity Lengths

    For different external cavity lengths, lasing wavelength variation of fiber grating external cavity semiconductor laser (FGECSL) with ambient temperature has been investigated theoretically, and the ...

    Shortest path problem of uncertain random network

    The shortest path problem is one of the most fundamental problems in network optimization. This paper is concerned with shortest path problems in non-deterministic environment, in which some arcs have...

    kgb档案压缩console版+源码

    A PAQ6 archive has a header, listing the names and lengths of the files it contains in human-readable format, followed by the compressed data. The first line of the header is "PAQ6 -m" where -m is the...

    NIST.FIPS.197

    This standard specifies the Rijndael algorithm ([3] and [4]), a symmetric block cipher that can process data blocks of 128 bits, using cipher keys with lengths of 128, 192, and 256 bits. Rijndael was ...

    NIST SP800-131Ar1.pdf

    The Recommendation addresses the use of algorithms and key lengths; the validation of cryptographic modules that utilize them is provided in [SP 800-131B]. The dates provided in SP 800-131A may ...

    NIST SP800-131A.pdf

    The Recommendation addresses the use of algorithms and key lengths; the validation of cryptographic modules that utilize them is provided in [SP 800-131B]. The dates provided in SP 800-131A may ...

    NIST SP800-131Ar2.pdf

    The Recommendation addresses the use of algorithms and key lengths; the validation of cryptographic modules that utilize them is provided in [SP 800-131B]. The dates provided in SP 800-131A may ...

Global site tag (gtag.js) - Google Analytics