-
Notifications
You must be signed in to change notification settings - Fork 5
/
318. Maximum Product of Word Lengths.java
39 lines (39 loc) · 1.18 KB
/
318. Maximum Product of Word Lengths.java
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
class Solution {
public int maxProduct(String[] words) {
int ans = Integer.MIN_VALUE;
final int n = words.length;
for (int i = 0; i < n; i++) {
String word1 = words[i];
HashSet<Character> set = this.prepareHashSet(word1);
int innerMax = Integer.MIN_VALUE;
for (int j = i + 1; j < n; j++) {
String word2 = words[j];
boolean status = false;
for (Character c : word2.toCharArray()) {
if (set.contains(c)) {
status = true;
break;
}
}
if (status == false) {
innerMax = Math.max(innerMax, word2.length());
}
}
ans = Math.max(ans, innerMax * word1.length());
}
return ans == Integer.MIN_VALUE ? 0 : ans;
}
private HashSet<Character> prepareHashSet(String word1) {
// TODO Auto-generated method stub
HashSet<Character> set = new HashSet<>();
for (Character c : word1.toCharArray()) {
set.add(c);
}
return set;
}
}