-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode_1239.c
34 lines (29 loc) · 994 Bytes
/
Leetcode_1239.c
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
#define MAX(a, b) ((a) > (b)) ? (a) : (b)
int maxLength(char** arr, int arrSize) {
int ans = 0;
unsigned *converted = calloc(arrSize, sizeof(unsigned));
unsigned *combination = calloc(1 << arrSize, sizeof(unsigned));
int k = 0, comb_len = 0;
for (int i = 0; i < arrSize; i++) {
unsigned tmp = 0U;
for (int j = 0; j < strlen(arr[i]); j++)
tmp |= 1 << (arr[i][j] - 'a');
if (strlen(arr[i]) != __builtin_popcount(tmp))
continue;
converted[k++] = tmp;
}
combination[comb_len++] = 0U;
for (int i = 0; i < k; i++) {
for (int j = comb_len - 1; j >= 0; j--) {
unsigned tmp = combination[j];
if (tmp & converted[i])
continue;
combination[comb_len++] = (tmp | converted[i]);
}
}
for (int i = 0; i < comb_len; i++)
ans = MAX(ans, __builtin_popcount(combination[i]));
free(converted);
free(combination);
return ans;
}