Skip to content

Commit fd0585a

Browse files
refactor 318
1 parent 8cabc69 commit fd0585a

File tree

1 file changed

+2
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+2
-24
lines changed

src/main/java/com/fishercoder/solutions/_318.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.Arrays;
4-
import java.util.HashSet;
5-
import java.util.Set;
6-
7-
/**318. Maximum Product of Word Lengths
8-
*
9-
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.
10-
11-
Example 1:
12-
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
13-
Return 16
14-
The two words can be "abcw", "xtfn".
15-
16-
Example 2:
17-
Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
18-
Return 4
19-
The two words can be "ab", "cd".
20-
21-
Example 3:
22-
Given ["a", "aa", "aaa", "aaaa"]
23-
Return 0
24-
No such pair of words.*/
253
public class _318 {
264
public static class Solution1 {
275
//Inspired by this awesome post: https://discuss.leetcode.com/topic/35539/java-easy-version-to-understand
@@ -38,15 +16,15 @@ public int maxProduct(String[] words) {
3816
String word = words[i];
3917
for (int j = 0; j < words[i].length(); j++) {
4018
values[i] |= 1 << (word.charAt(j)
41-
- 'a');//the reason for left shift by this number "word.charAt(j) -'a'" is for 'a', otherwise 'a' - 'a' will be zero and 'a' will be missed out.
19+
- 'a');//the reason for left shift by this number "word.charAt(j) -'a'" is for 'a', otherwise 'a' - 'a' will be zero and 'a' will be missed out.
4220
}
4321
}
4422
int maxProduct = 0;
4523
for (int i = 0; i < words.length; i++) {
4624
for (int j = 0; j < words.length; j++) {
4725
//check if values[i] AND values[j] equals to zero, this means they share NO common chars
4826
if ((values[i] & values[j]) == 0
49-
&& words[i].length() * words[j].length() > maxProduct) {
27+
&& words[i].length() * words[j].length() > maxProduct) {
5028
maxProduct = words[i].length() * words[j].length();
5129
}
5230
}

0 commit comments

Comments
 (0)