Hi
Please Help me to find out mistake in the code,It is failing one test case (Test Case:0)
Code Failing One Test Case
The code is failing test cases as the approach is wrong. On first glance it may look like a modification of LIS but it is actually a question of bit manipulation.
This is the actual logic of the code:-
class Solution {
public:
int maxLength(vector<string>& arr)
{
int n = arr.size();
int bin[n];
for(int i = 0; i < n; i++)
{
int b = 0;
for(int j = 0; j < arr[i].length(); j++)
{
if((b & (1 << (arr[i][j] - 'a'))) != 0)
{
b = 0;
break;
}
b |= (1 << (arr[i][j] - 'a'));
}
bin[i] = b;
}
int ans = 0, len, temp;
bool flag;
string res;
for(int i = 0; i < (1 << n); i++)
{
temp = 0, flag = 0, res = "";
for(int j = 0; j < n; j++)
{
if(i & (1 << j))
{
if((temp & bin[j]) == 0)
{
temp |= bin[j];
if(bin[j])
res += arr[j];
}
else
{
flag = 1;
break;
}
}
}
ans = max(ans, int(res.length()));
}
return ans;
}
};
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.