generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
40 lines (34 loc) · 977 Bytes
/
solution.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
35
36
37
38
39
40
#include <stdint.h>
#include <stdlib.h>
int fn(uint32_t* uniques, int uniquesSize, int i, uint32_t unique);
int maxLength(char** arr, int arrSize) {
uint32_t* uniques = malloc(sizeof(uint32_t) * arrSize);
for (int i = 0; i < arrSize; ++i) {
uniques[i] = 0;
char* c = arr[i];
while (*c != 0) {
uint32_t div = 1 << (*c - 'a');
if ((uniques[i] & div) != 0) {
uniques[i] = 0;
break;
}
uniques[i] |= div;
++c;
}
}
const int len = fn(uniques, arrSize, 0, 0);
free(uniques);
return len;
}
int fn(uint32_t* uniques, int uniquesSize, int i, uint32_t unique) {
if (i == uniquesSize) {
return __builtin_popcount(unique);
}
if ((unique & uniques[i]) == 0) {
int pick = fn(uniques, uniquesSize, i + 1, unique | uniques[i]);
int notPick = fn(uniques, uniquesSize, i + 1, unique);
return pick > notPick ? pick : notPick;
} else {
return fn(uniques, uniquesSize, i + 1, unique);
}
}