Skip to content

Commit 3704824

Browse files
authored
Create 1239.Maximum-Length-of-a-Concatenated-String-with-Unique-Characters.cpp
1 parent 15f64ac commit 3704824

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
typedef long long ll;
2+
class Solution {
3+
int ret = 0;
4+
public:
5+
int maxLength(vector<string>& arr)
6+
{
7+
vector<ll>codes;
8+
for (int i=0; i<arr.size(); i++)
9+
{
10+
ll code = 0;
11+
int flag = 1;
12+
for (auto ch: arr[i])
13+
{
14+
if ((code>>(ch-'a'))&1)
15+
{
16+
flag = 0;
17+
break;
18+
}
19+
code += (1<<(ch-'a'));
20+
}
21+
if (flag)
22+
codes.push_back(code);
23+
}
24+
25+
ll bits = 0;
26+
for (int i=0; i<codes.size(); i++)
27+
dfs(codes, i, codes[i]);
28+
return ret;
29+
}
30+
31+
void dfs(vector<ll>codes, int i, ll bits)
32+
{
33+
ret = max(ret, __builtin_popcount(bits));
34+
for (int j=i+1; j<codes.size(); j++)
35+
{
36+
if ((bits & codes[j])==0)
37+
{
38+
dfs(codes, j, bits + codes[j]);
39+
}
40+
}
41+
}
42+
};

0 commit comments

Comments
 (0)