-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode1239.cpp
29 lines (29 loc) · 936 Bytes
/
LeetCode1239.cpp
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
class Solution
{
public:
int maxLength(vector<string> &arr)
{
vector<int> dp = {0};
int n = arr.size();
int ans = 0;
for (int i = 0; i < n; i++)
{
int checkDupInCurrentString = 0, currentBits = 0;
for (char ch : arr[i])
{
checkDupInCurrentString |= (currentBits & 1 << (ch - 'a'));
currentBits |= 1 << (ch - 'a');
}
if (checkDupInCurrentString > 0)
continue;
for (int i = dp.size() - 1; i >= 0; i--) // iterating in reverse due to having size increased if there is not duplication between subsequent string
{
if (dp[i] & currentBits)
continue;
dp.push_back(dp[i] | currentBits);
ans = max(ans, __builtin_popcount(dp[i] | currentBits));
}
}
return ans;
}
};