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.

分析:

怎么判断两个string没有共同的letter。 我们可以用int的每一个位来表示是否某一个letter出现在某一个字符串中。如果两个字符串的int值AND以后为0,很明显,它们是没有公共letter.

 public class Solution {
public int maxProduct(String[] words) {
if (words == null || words.length <= ) return ; int[] wordInfo = new int[words.length];
for (int i = ; i < words.length; i++) {
for (int j = ; j < words[i].length(); j++) {
// each letter is the 1 bit in the number
wordInfo[i] = wordInfo[i] | ( << words[i].charAt(j) - 'a');
}
}
int maxProduct = ;
for (int i = ; i < words.length; i++) {
for (int j = i + ; j < words.length; j++) {
if (((wordInfo[i] & wordInfo[j]) == )) {
maxProduct = Math.max(maxProduct, words[i].length() * words[j].length());
}
}
} return maxProduct;
}
}

Maximum Product of Word Lengths的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  2. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  3. 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 tw ...

  4. 318. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  5. 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  6. Java [Leetcode 318]Maximum Product of Word Lengths

    题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...

  7. leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)

    https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...

  8. [Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  9. [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 tw ...

随机推荐

  1. Xamarin.Forms入门学习路线

    Xamarin 介绍 Xamarin是一套跨平台解决方案,目的是使用C#语言创造原生的iOS,Android,Mac和Windows应用. Xamarin的三个优势: Xamarin App拥有原生A ...

  2. Struts2 数据校验流程

  3. Google-解决在调试页面或者js时总是提示烦恼的断点问题

    按F12键,然后切换到Source标签,看底下的那个跟暂停一样的图标是不是变成蓝色或紫色了? 如果是蓝色或者紫色,则把他切换到“灰色”状态(点击图标就会切换成不同的状态.或者可能是其他颜色状态),如下 ...

  4. druid(德鲁伊)数据源的使用和配置 阿里出品

    pom.xml <dependency>     <groupId>com.alibaba</groupId>     <artifactId>drui ...

  5. c++内存分配(new和delete)

    c中malloc和free是函数,包含在stdlib.h头文件中,分配成功返回指针,失败返回空指针. 与new的区别是: 1,malloc与free是C++/C语言的标准库函数,new/delete是 ...

  6. rockmongo用法

    .简单查询 //xid=560870 and type=video { , "type": "video" } //查询数组中的数据 array( " ...

  7. Python初学笔记

    一.安装:直接通过软件管理程序,搜索Python,安装:安装过程中自定义路径,有个选项类似“add Python3.5 to Path”,勾选后便可以在cmd命令窗口,通过输入Python,启动Pyt ...

  8. 初学Struts2-自定义拦截器及其配置

    自定义拦截器,首先新建一个继承自AbstractInterceptor类的类,然后重写intercept方法,代码如下 public class HelloInterceptor extends Ab ...

  9. ios 随机色 宏定义

    #define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] #d ...

  10. 工作者对象HttpWorkerRequest

    在ASP.NET中,用于处理的请求,需要封装为HttpWorkerRequest类型的对象.该类为抽象类,定义在命名空间System.Web下. #region Assembly System.Web ...