Java 100% Faster | 1 ms solution | Explained
Approach\nExplanation.\n\nOccur is an 2d array which stores occurrences of all digits of each number (power of 2). \n\nExample: 2^16 = 65536, array with occuren
# Approach\nExplanation.\n\n**Occur** is an 2d array which stores occurrences of all digits of each number (power of 2). \n\nExample: 2^16 = 65536, array with occurences of digits for it will be: [0,0,0,1,0,2,2,0,0,0], because we have one occurence of 3, two occurences of 5, and two occurences of 6.\n\n**Occur** is $$static$$ because I fill it only once and use for each test case.\n\nThe names of methods are self explainable.\n\n# Code\n```\nclass Solution {\n\n    static byte[][] ocurr;\n\n    public boolean reorderedPowerOf2(int n) {\n        if (n == 1) return true;\n        if (ocurr == null) {\n            ocurr = fillOccurencesForEachPowerOf2();\n        }\n\n        byte[] freq = countFreqs(n);\n        for (byte[] oc : ocurr) {\n            if (areSame(oc, freq)) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    private byte[][] fillOccurencesForEachPowerOf2() {\n        byte[][] res = new byte[30][10];\n        for (int i = 0; i < res.length; i++) {\n            int pow = (int) Math.pow(2, i);\n            res[i] = countFreqs(pow);\n        }\n        return res;\n    }\n\n    private byte[] countFreqs(int pow) {\n        byte[] freq = new byte[10];\n        while (pow > 0) {\n            int digit = pow % 10;\n            //increment digits frequency for number pow\n            freq[digit]++;\n            pow /= 10;\n        }\n        return freq;\n    }\n\n    private boolean areSame(byte[] oc, byte[] freq) {\n        for (int i = 0; i < oc.length; i++) {\n            if (oc[i] != freq[i]) return false;\n        }\n        return true;\n    }\n}\n```
['Java']