Python 27 ms (faster than 100%), new idea to improve other's solutions
Like others solutions, i use the same idea of counting the digits and then compare with digits of candidates: 1, 2, 4, 8, 16, 32, 64 ... 2^30.\nBut i noticed th
Like others solutions, i use the same idea of counting the digits and then compare with digits of candidates: ```1, 2, 4, 8, 16, 32, 64 ... 2^30```.\nBut i noticed that if our input is for example ```251``` we need to compare it only with power of 2 numbers that have exact 3 digits: ```128, 256, 512```, but how can we find out what numbers we need?\nLet\'s look for numbers that are powers of 2, their length(number of digits) and their power:\n```\nnumbers: 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192\nlength:  1 1 1 1  2  2  2   3   3   3    4    4    4    4 \npower:   0 1 2 3  4  5  6   7   8   9   10   11   12   13\n```\nLet\'s see the pattern between ```length``` and ```power```:\nIf length of number is 1, power lies between 0 and 3, if length is 2 , power lies between 4 and 6 and etc.\nSo all we need is to find the formula to know in what boundaries numbers lie.\nFirst of all what i thought is that power lies in ```((length - 1) * 3 + 1, length * 3 + 1)```, for example for ```length = 2``` range will be ```range(4, 7) = (4, 5, 6)``` is what we needed, but this formula doesn\'t work for ```length = 1, 4, 7 etc```, because every third length(or every 10th power) there are 4 numbers with this length, that ruins the formula, so i thought "okay, what if we will add 1 every time when we our numbers length reaches 1, 4, 7 etc...", or in another words: ```add length // 3```, but now our length of range will always be 4, that is still better than 30.\nSo now our formula for range is ```((length - 1) * 3 + length // 3, length * 3 + length // 3 + 1)```, maybe there\'s a way to simplify the formula, but i think it\'s fine, so the final code is:\n```python\nclass Solution:\n    def reorderedPowerOf2(self, n: int) -> bool:\n        from collections import Counter\n        length = len(str(n))\n        c = Counter(str(n))\n        for i in range((length-1) * 3 + length // 3, length * 3 + length // 3 + 1):\n            candidate = str(1 << i)\n            if c == Counter(candidate):\n                return True\n        return False\n```\n\nIf we replace the Counter with sorted, it can be even faster:\n```python\nclass Solution:\n    def reorderedPowerOf2(self, n: int) -> bool:\n        length = len(str(n))\n        c = sorted(str(n))\n        for i in range((length-1) * 3 + length // 3, length * 3 + length // 3 + 1):\n            if c == sorted(str(1 << i)):\n                return True\n        return False\n```\n![image](https://assets.leetcode.com/users/images/4b1d106b-c511-4fc9-af8c-8c610e325fab_1661479742.4333918.png)\n\n```\nP.S. The formula was obtained by trial and error, \nbut i came up with the main idea with connection length and power.\n```\n\n```\nP.P.S. Feel free to ask about something if it\'s unclear, \nwrite an improve ment if you have an idea, \nor point on mistakes(including english) :)\n```\n\n
['Math', 'Sorting', 'Python']