[Javascript] Bitwise Mask + Prefix Xor comments + explanation
First off, let\'s deal with the palindromes. Any possible number of switches is allowed so I only have to deal with the frequency of the characters inside the s
First off, let\'s deal with the palindromes. Any possible number of switches is allowed so I only have to deal with the frequency of the characters inside the string. Any string will be palindrome if every frequency is even, except maybe one. \n\nSo essentially, there are only 11 possible states Im interested in\n1000000000,010000000,....,000000001,000000000\n which mean that The only odd frequency seen so far is of the number i\n  0<=i<=9\n Prefix Xor part: \nWell Every time i come across any new state, I save it as a key with the value being the index I first met that accumulative xor. Then, any time in the future, i come across the same state that means that inbetween value\n\n```\nvar longestAwesome = function(s) {\n    // freq starts with 0:0 because 9 0s is also a state and if I come across a \n    // 0 down the road, that means that the whole array up to index i is of the required type\n    let firstIndex={0:0}, result=-1, curr=0\n    for (let i = 0; i < s.length; i++) {\n       curr^= 1<<s[i]\n       // Check if you have seen curr^0=curr before, \n\t   // because that would make the inbetween elements\' xor = 000000000\n       if(firstIndex[curr]!==undefined)\n            result=Math.max(result,i-firstIndex[curr]+1)\n       // Check all the other xors, because that would make \n\t   // the inbetween elements of the required type (100000000,010000000,......,000000001)\n       for (let j = 0; j <10; j++) {\n            let ele=1<<j\n            if(firstIndex[curr^ele]!==undefined)\n                //i-firstIndex[curr^ele] because on freq I saved\n\t\t\t\t//the smallest index where I last met curr^ele\n                result=Math.max(result,i-firstIndex[curr^ele]+1)\n       }\n       if(firstIndex[curr]===undefined)\n\t\t   firstIndex[curr]=i+1// +1 cos 0th place is for my 0 state\n    }\n \n    return result\n};\n```\n
['Bit Manipulation', 'JavaScript']