Maximum Product of Word Lengths
2016-09-03 12:35
99 查看
Given a string array
Example 1:
Given
Return
The two words can be
Example 2:
Given
Return
The two words can be
Example 3:
Given
Return
No such pair of words.
要保证2个条件
/*
* 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".
*
* */
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int maxProduct(String[] words) {
if(words.length==0)
return 0;
int wordsLen = words.length;
//int[] word = new int[wordsLen];//
int[] values = new int[wordsLen];//来记录每一个单词中字母出现的情况
for(int i = 0; i<wordsLen; i++)
{
String word = words[i];
values[i] = 0;
for(int j = 0; j<word.length(); j++)
{
values[i] |= 1<<(word.charAt(j)-'a');//通过对1的左移操作相当于把26个字母放在不同的位置上,a放在第一位
//b放在第二位,。。。。z放在第26位
//然后通过对一个单词遍历后便能,便能找到有哪些个字母出现过,其位置上就为1,只要求出现过,并不用
//关注出现多少次,所以进行或运算就性
}
}
int max = 0;
for(int i = 0; i<wordsLen-1; i++)
{
for(int j = i + 1; j<wordsLen; j++)
{
if((values[i]&values[j])==0)//相与后为0则说明在同位置上并没有出现相同的字母
{
if(words[i].length()*words[j].length()>max)
{
max = words[i].length()*words[j].length();
}
}
}
}
return max;
}
}
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.
要保证2个条件
/*
* 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".
*
* */
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int maxProduct(String[] words) {
if(words.length==0)
return 0;
int wordsLen = words.length;
//int[] word = new int[wordsLen];//
int[] values = new int[wordsLen];//来记录每一个单词中字母出现的情况
for(int i = 0; i<wordsLen; i++)
{
String word = words[i];
values[i] = 0;
for(int j = 0; j<word.length(); j++)
{
values[i] |= 1<<(word.charAt(j)-'a');//通过对1的左移操作相当于把26个字母放在不同的位置上,a放在第一位
//b放在第二位,。。。。z放在第26位
//然后通过对一个单词遍历后便能,便能找到有哪些个字母出现过,其位置上就为1,只要求出现过,并不用
//关注出现多少次,所以进行或运算就性
}
}
int max = 0;
for(int i = 0; i<wordsLen-1; i++)
{
for(int j = i + 1; j<wordsLen; j++)
{
if((values[i]&values[j])==0)//相与后为0则说明在同位置上并没有出现相同的字母
{
if(words[i].length()*words[j].length()>max)
{
max = words[i].length()*words[j].length();
}
}
}
}
return max;
}
}
相关文章推荐
- [LeetCode 318] Maximum Product of Word Lengths
- Leetcode: Maximum Product of Word Lengths
- leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
- Maximum Product of Word Lengths
- Maximum Product of word lengths
- Leetcode: Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- LeetCode 318 Maximum Product of Word Lengths (位压缩)
- Maximum Product of Word Lengths
- LeetCode 318: Maximum Product of Word Lengths
- Leetcode: Maximum Product of Word Lengths
- leetcode(318):Maximum Product of Word Lengths
- leetcode 318: Maximum Product of Word Lengths
- leetcode Maximum Product of Word Lengths
- LeetCode 318 Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- *Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- LeetCode:Maximum Product of Word Lengths
- [LeetCode]Maximum Product of Word Lengths(Java)