Reordered Power of 2 | JS, Python, Java, C++ | Easy, Short Solution w/ Explanation
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\n\n---\n\n#### Idea:\n
*(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful,* ***please upvote*** *this post.)*\n\n---\n\n#### ***Idea:***\n\nThe easiest way to check if two things are shuffled versions of each other, which is what this problem is asking us to do, is to sort them both and the compare the result.\n\nIn that sense, the easiest solution here is to do exactly that: we can convert **N** to an array of its digits, sort it, then compare that result to the result of the same process on each power of **2**.\n\nSince the constraint upon **N** is **10e9**, we only need to check powers in the range **[0,29]**.\n\nTo make things easier to compare, we can always **join()** the resulting digit arrays into strings before comparison.\n\nThere are ways to very slightly improve the run time and memory here, but with an operation this small, it\'s honestly not very necessary.\n\n---\n\n#### ***Implementation:***\n\nPython can directly compare the lists and Java can directly compare the char arrays without needing to join them into strings. C++ can sort the strings in-place without needing to convert to an array.\n\n---\n\n#### ***Javascript Code:***\n\nThe best result for the code below is **72ms / 38.8MB** (beats 100% / 44).\n```javascript\nvar reorderedPowerOf2 = function(N) {\n    let res = N.toString().split("").sort().join("")\n    for (let i = 0; i < 30; i++)\n        if ((1 << i).toString().split("").sort().join("") === res) return true\n    return false\n};\n```\n\n---\n\n#### ***Python Code:***\n\nThe best result for the code below is **24ms / 14.1MB** (beats 98% / 76%).\n```python\nclass Solution:\n    def reorderedPowerOf2(self, N: int) -> bool:\n        res = sorted([int(x) for x in str(N)])\n        for i in range(30):\n            if sorted([int(x) for x in str(1 << i)]) == res: return True\n        return False\n```\n\n---\n\n#### ***Java Code:***\n\nThe best result for the code below is **1ms / 35.8MB** (beats 97% / 88%).\n```java\nclass Solution {\n    public boolean reorderedPowerOf2(int N) {\n        char[] res1 = String.valueOf(N).toCharArray();\n        Arrays.sort(res1);\n        for (int i = 0; i < 30; i++) {\n            char[] res2 = String.valueOf(1 << i).toCharArray();\n            Arrays.sort(res2);\n            if (Arrays.equals(res1, res2)) return true;\n        }\n        return false;\n    }\n}\n```\n\n---\n\n#### ***C++ Code:***\n\nThe best result for the code below is **0ms / 5.8MB** (beats 100% / 99%).\n```c++\nclass Solution {\npublic:\n    bool reorderedPowerOf2(int N) {\n        string res1 = to_string(N);\n        sort(res1.begin(), res1.end());\n        for (int i = 0; i < 30; i++) {\n            string res2 = to_string(1 << i);\n            sort(res2.begin(), res2.end());\n            if (res1 == res2) return true;\n        }\n        return false;\n    }\n};\n```
[]