-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path318.cpp
More file actions
40 lines (36 loc) · 1004 Bytes
/
318.cpp
File metadata and controls
40 lines (36 loc) · 1004 Bytes
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
40
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxProduct(vector<string>& words) {
auto stringToInt = [](const string& str) -> int {
int result = 0;
for(char c : str) {
result |= (1 << (c - 'a'));
}
return result;
};
const int size = words.size();
vector<int> nums(size);
for(int i = 0; i < size; ++i) {
nums[i] = stringToInt(words[i]);
}
int result = 0;
for(int i = 0; i < size; ++i) {
for(int j = i + 1; j < size; ++j) {
if((nums[i] & nums[j]) == 0) {
result = max(result, (int)words[i].length()(int)words[j].length());
}
}
}
return result;
}
};
int main() {
Solution solution;
vector<string> words = {
"abcw","baz","foo","bar","xtfn","abcdef"
};
cout << solution.maxProduct(words) << endl;
return 0;
}