[LeetCode The Hard Way] Easy Sorting with Explanation
Please check out LeetCode The Hard Way for more solution explanations and tutorials. If you like it, please give a star and watch my Github Repository.\n\n---\n
Please check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. If you like it, please give a star and watch my [Github Repository](https://github.com/wingkwong/leetcode-the-hard-way).\n\n---\n\n```cpp\nclass Solution {\npublic:\n    string sortStr(int n) {\n        // since the input is an integer,\n        // we convert it to a string first\n        string t = to_string(n);\n        // use STL to sort\n        sort(t.begin(), t.end());\n        // return the string\n        return t;\n    }\n    \n    // the idea is to sort `n` and compare all sorted power of two\n    // if they are matched, then it means they can be reordered to each other\n    bool reorderedPowerOf2(int n) {\n        // since the sorted string of n is always same\n        // so we convert it here instead of doing it in the loop\n        string s = sortStr(n);\n        for (int i = 0; i < 30; i++) {\n            // power of 2 = 1 << i\n            // we sort each power of 2 string\n            string t = sortStr(1 << i);\n            // and compare with `s`\n            // if they are matched, then return true\n            if (s == t) return true;\n        }\n        // otherwise it is not possible to reorder to a power of 2\n        return false;\n    }\n};\n```
['Bit Manipulation', 'C', 'Sorting', 'C++']