-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxLength.cpp
More file actions
25 lines (23 loc) · 751 Bytes
/
maxLength.cpp
File metadata and controls
25 lines (23 loc) · 751 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
class Solution {
public:
int maxLength(vector<string>& arr) {
int max_len = 0;
vector<bitset<26>> unique;
for (auto &s : arr) {
bitset<26> bin;
for (char ch : s) bin.set(ch - 'a');
if (bin.count() == s.size())
unique.push_back(bin);
}
vector<bitset<26>> concat = {bitset<26>()};
for (auto& u : unique) {
for (int i = concat.size() - 1; i >= 0; i--) {
if ((concat[i] & u).none()) {
concat.push_back(concat[i] | u);
max_len = max(max_len, static_cast<int>(concat[i].count() + u.count()));
}
}
}
return max_len;
}
};