-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path318.cpp
39 lines (39 loc) · 1.14 KB
/
318.cpp
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
// higher-efficient.cpp
class Solution {
public:
int maxProduct(vector<string> &words) {
int len = 0;
vector<int> comp(words.size(), 0);
for (int i = 0; i < words.size(); ++i) {
string &str = words[i];
for (int j = 0; j < str.size(); ++j)
comp[i] |= 1 << (str[j] - 'a');
}
for (int i = 0; i < words.size(); ++i)
for (int j = i + 1; j < words.size(); ++j)
if ((comp[i] & comp[j]) == 0)
len = max(len, (int)(words[i].size() * words[j].size()));
return len;
}
};
// lower-efficient.cpp
class Solution2 {
public:
int maxProduct(vector<string> &words) {
int len = 0;
for (int i = 0; i < words.size(); ++i) {
unordered_set<char> hashset(words[i].begin(), words[i].end());
for (int j = i + 1; j < words.size(); ++j) {
bool withoutCommon = true;
for (int k = 0; k < words[j].size(); ++k)
if (hashset.find(words[j][k]) != hashset.end()) {
withoutCommon = false;
break;
}
len = withoutCommon ? max(len, (int)(words[i].size() * words[j].size()))
: len;
}
}
return len;
}
};