You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/fishercoder/solutions/_318.java
+2-24Lines changed: 2 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,5 @@
1
1
packagecom.fishercoder.solutions;
2
2
3
-
importjava.util.Arrays;
4
-
importjava.util.HashSet;
5
-
importjava.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.*/
25
3
publicclass_318 {
26
4
publicstaticclassSolution1 {
27
5
//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) {
38
16
Stringword = words[i];
39
17
for (intj = 0; j < words[i].length(); j++) {
40
18
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.
42
20
}
43
21
}
44
22
intmaxProduct = 0;
45
23
for (inti = 0; i < words.length; i++) {
46
24
for (intj = 0; j < words.length; j++) {
47
25
//check if values[i] AND values[j] equals to zero, this means they share NO common chars
0 commit comments