Skip to content

Commit 6605ba9

Browse files
committed
maximum product of word length
1 parent a40c865 commit 6605ba9

File tree

3 files changed

+74
-33
lines changed

3 files changed

+74
-33
lines changed

.idea/workspace.xml

Lines changed: 34 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,4 @@ codes may not be optimized
387387
1. [Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/description/)
388388
1. [Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/description/)
389389
1. [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/description/)
390+
1. [Maximum Product of Word Length](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.chen0040.leetcode.day18.medium;
2+
3+
4+
/**
5+
* Created by xschen on 13/8/2017.
6+
*
7+
* link: https://leetcode.com/problems/maximum-product-of-word-lengths/description/
8+
*/
9+
public class MaximumProductOfWordLength {
10+
public class Solution {
11+
public int maxProduct(String[] words) {
12+
int[][] vectors = new int[words.length][];
13+
for(int i=0; i < words.length; ++i) {
14+
String word = words[i];
15+
int[] vec = new int[26];
16+
for(int j = 0; j < word.length(); ++j){
17+
int d = (int)word.charAt(j) - (int)'a';
18+
vec[d] = 1;
19+
}
20+
vectors[i] = vec;
21+
}
22+
23+
int maxLength = 0;
24+
for(int i=0; i < words.length; ++i) {
25+
for(int j=i+1; j < words.length; ++j){
26+
int dp = 0;
27+
for(int k=0; k < 26; ++k){
28+
dp += vectors[i][k] * vectors[j][k];
29+
}
30+
if(dp == 0) {
31+
maxLength = Math.max(maxLength, words[i].length() * words[j].length());
32+
}
33+
}
34+
}
35+
36+
return maxLength;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)