<p>You are given a string <code>s</code>. An <strong>awesome</strong> substring is a non-empty substring of <code>s</code> such that we can make any number of swaps in order to make it a palindrome.</p>

<p>Return <em>the length of the maximum length <strong>awesome substring</strong> of</em> <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;3242415&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> &quot;24241&quot; is the longest awesome substring, we can form the palindrome &quot;24142&quot; with some swaps.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;12345678&quot;
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;213123&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> &quot;213123&quot; is the longest awesome substring, we can form the palindrome &quot;231132&quot; with some swaps.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> consists only of digits.</li>
</ul>

/*

   note:
     1. the difficulty here is the substring is s[i..j] instead of any pick
        and the swap is performed after substring
        
   thought:
     1. since it can make any number of swaps of a substring = any permutation is OK for a substring
        => find the substring with "all pair"s or "all pairs + 1 single"
     
   method:
     1. count + brute force: O(n^2)
        just try every substring to see if it match thought 1
        
     2. hash for bits: O(n)
        use bit to represent s[0..i] and h[bit] = v, v is the pair num
        a first thought is h[bit] ={v1, v2, v3}, but the first one has the min v
        => curr max(v - h[bit]) is the best, said v from bitv,
           bit is bit=bitv
                  or said bb = bit^bitv, bb&(bb - 1) == 0 (only 1 bit is 1 in bb)
     
        
*/
class Solution {
public:
    // 5630 
    // 1101 
    // 1001 6
    // 1011 3
    // 1010 0
    // 1110 6
    // 1010 6
    // 1000 3
    int longestAwesome(string s) {
        static const int INVALID_I = -2;
        static const int SIZE = 1<<11;
        int n = s.length();
        vector<int> count(SIZE, INVALID_I);
        
        int curr = 0, res = 0, p_num = 0;
        count[0] = -1;
        for (int i = 0; i < n; ++i) {
            curr ^= 0x1<<(s[i] - '0');
            
            if (count[curr] == INVALID_I)
                count[curr] = i;
            else
                res = max(res, i - count[curr]);
            
            for (int j = 1; j < SIZE; j <<= 1)
                if (count[curr^j] != INVALID_I)
                    res = max(res, i - count[curr^j]);
        }
        
        return res;
    }
};
12400