- 浏览: 184808 次
- 性别:
- 来自: 济南
文章分类
最新评论
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)。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 442There are n bulbs that are init ...
相关推荐
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...
Product of Word Lengths 【这个题目LTE 复杂度已经降下了】 最长不重复字符串3. Longest Substring Without Repeating Characters ----2016.10.08 移动0到末尾 283. Move Zeroes 翻转二叉树 226. Invert Binary ...
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-...
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 ...
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/...
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, Maximum Likelihood Estimation)相关的学习资料,由斯坦福大学提供的部分课程内容组成。在这个压缩包中,主要包含了一份名为“MLE_MAP_Part1.pdf...
leetcode ...product of word lengths 一开始想到了排序,然后从大到小进行遍历,但是仍然超时,后来在每次判断是否有相同字符之前,先判断是否乘积大于目前的最大值,可以AC,目前看来,排序并没有什么
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 ...
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 ...
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 ...
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...
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...
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 ...
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 ...
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 ...
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 ...