Java efficient solution with explanation
\nclass Solution {\n    \n    public int longestAwesome(String s) {\n        \n        // We only need to care about if a digit appears odd or even number of ti
```\nclass Solution {\n    \n    public int longestAwesome(String s) {\n        \n        // We only need to care about if a digit appears odd or even number of times.\n        // Since the total number of digits is 10, the number of possible states is\n        // (1<<10) == 1024\n        int[] idxFirstAppearance = new int[1<<10]; // index where a state first appears\n        Arrays.fill(idxFirstAppearance, Integer.MIN_VALUE);\n        idxFirstAppearance[0] = -1;\n        \n        int mask = 0; // initial state\n        int ans = 0;\n        \n        for (int i = 0; i < s.length(); i++) {\n            \n            int idx = s.charAt(i) - \'0\';\n            \n            // Case 1: all digits appear even number of times\n            mask ^= (1 << idx); // update state\n            \n            if (idxFirstAppearance[mask] == Integer.MIN_VALUE)\n                idxFirstAppearance[mask] = i;\n            else\n                ans = Math.max(ans, i-idxFirstAppearance[mask]);\n            \n            // Case 2: all digits but one appear even number of times\n            for (int j=0; j<10; j++) {\n                \n                int temp = mask ^ (1 << j);\n                \n                ans = Math.max(ans, i-idxFirstAppearance[temp]);\n            }\n        }\n        \n        return ans;\n    }\n    \n}\n```
['Bitmask']