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
+22-94Lines changed: 22 additions & 94 deletions
Original file line number
Diff line number
Diff line change
@@ -23,113 +23,41 @@ Given a string array words, find the maximum value of length(word[i]) * length(w
23
23
Return 0
24
24
No such pair of words.*/
25
25
publicclass_318 {
26
-
//Inspired by this awesome post: https://discuss.leetcode.com/topic/35539/java-easy-version-to-understand
27
-
//Idea: this question states that all words consisted of lower case (total only 26 unique chars),
28
-
//this is a big hint that we could use integer (total 32 bits) to represent each char
29
-
//values[i] means how many unique characters this string words[i] has
30
-
publicintmaxProduct(String[] words) {
31
-
if (words == null || words.length == 0) {
32
-
return0;
33
-
}
34
-
intlen = words.length;
35
-
int[] values = newint[len];
36
-
for (inti = 0; i < words.length; i++) {
37
-
Stringword = words[i];
38
-
for (intj = 0; j < words[i].length(); j++) {
39
-
values[i] |= 1 << (word.charAt(j) - '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.
40
-
}
41
-
}
42
-
intmaxProduct = 0;
43
-
for (inti = 0; i < words.length; i++) {
44
-
for (intj = 0; j < words.length; j++) {
45
-
//check if values[i] AND values[j] equals to zero, this means they share NO common chars
0 commit comments