class Solution:
def longestAwesome(self, s: str) -> int:
""" tc O(KN) sc O(2^K), K: unique characters in string, here K = 10
Note, since palindrom allow up to one number with odd count , check all masks different from current one by one bit
=> if two masks are different by one bit, there is one odd cnt in the substring ????
"""
res,mask,n = 0,0,len(s)
seen = [n]*1024
seen[0] =-1 # position for 0, mask is -1
for i in range(n):
#(1) update mask with current char
mask ^= 1 << int(s[i])
#(3.1) check cases allow one number with odd count
# by using any number as middle char in palindrom, and check if its seen before
for j in range(10):
res = max(res,i-seen[mask^(1<<j)])
#(3.2) or non odd middle character
res = max(res,i-seen[mask])
#(2) keep mask's index if it is the first time to show up
seen[mask] = min(seen[mask],i)
return res