-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumProductofWordLengths.java
More file actions
51 lines (51 loc) · 1.64 KB
/
MaximumProductofWordLengths.java
File metadata and controls
51 lines (51 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package medium;
/**
* ClassName: MaximumProductofWordLengths.java
* Author: chenyiAlone
* Create Time: 2019/5/29 15:53
* Description: No.318
* 思路:
* 1. 题目限制了为小写字母,所以 26 个小写字母使用二进制表示也就 2^26
* 2. 判断两个字符串是否有有相同字符 | nums[i] + nums[j] == (nums[i] | nums[j])
*
*
* 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:
*
* Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
* Output: 16
* Explanation: The two words can be "abcw", "xtfn".
* Example 2:
*
* Input: ["a","ab","abc","d","cd","bcd","abcd"]
* Output: 4
* Explanation: The two words can be "ab", "cd".
* Example 3:
*
* Input: ["a","aa","aaa","aaaa"]
* Output: 0
* Explanation: No such pair of words.
*/
public class MaximumProductofWordLengths {
public int maxProduct(String[] words) {
int res = 0;
int len;
if ((len = words.length) < 2)
return res;
int[] nums = new int[len];
for (int i = 0; i < len; i++) {
String s = words[i];
for (char c : s.toCharArray()) {
nums[i] |= (1 << (c - 'a'));
}
}
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (nums[i] + nums[j] == (nums[i] | nums[j]))
res = Math.max(res, words[i].length() * words[j].length());
}
}
return res;
}
}