[JavaScript] Easy to understand - 4 solutions
For this problem, there are 2 main strategies to solve it:\n- One is following the rules in the description and try every possible option.\n- Another one is to 
For this problem, there are 2 main strategies to solve it:\n- One is following the rules in the description and try every possible option.\n- Another one is to try to find a way to serialize the number and check whether the serialization for `N` matches any serialization of power of 2.\n\n## SOLUTION 1\n\nFirst, we try the most straight forward solution - validate every reordered sequence of `N` and check if it\'s the power of 2.\n\n```js\nconst set = new Set();\nfor (let cur = 1; cur < 10 ** 9; cur <<= 1) {\n  set.add(String(cur));\n}\n\nconst validate = (left, cur = "") => {\n  if (left.length === 1) return set.has(cur + left[0]);\n  for (let i = 0; i < left.length; ++i) {\n    const next = left.slice(0, i) + left.slice(i + 1);\n    if (validate(next, cur + left[i])) return true;\n  }\n  return false;\n};\n\nconst reorderedPowerOf2 = (n) => validate(String(n));\n```\n\n## SOLUTION 2\n\nWe could find out all the possible sequences for the power of 2 between 1 and 10 ** 9 first. And then just check the mapping for the result.\n\n```js\nconst set = new Set();\n\nconst permute = (left, cur = "") => {\n  if (left.length === 1) {\n    set.add(Number(cur + left[0]));\n    return;\n  }\n  for (let i = 0; i < left.length; ++i) {\n    if (left[i] === "0" && cur.length === 0) continue;\n    const next = left.slice(0, i) + left.slice(i + 1);\n    permute(next, cur + left[i]);\n  }\n};\n\nfor (let cur = 1; cur < 10 ** 9; cur <<= 1) {\n  permute(String(cur));\n}\n\nconst reorderedPowerOf2 = (n) => set.has(n);\n```\n\n## SOLUTION 3\n\nThis solution follows the second strategy. We achieve serialization by counting the occurrences of each number.\n\n```js\nconst set = new Set();\n\nconst serialize = str => {\n  const count = new Uint8Array(10);\n  const BASE = 48;\n  for (let i = 0; i < str.length; ++i) {\n    ++count[str.charCodeAt(i) - BASE];\n  }\n  return count.join(\':\');\n};\n\nfor (let cur = 1; cur < 10 ** 9; cur <<= 1) {\n  set.add(serialize(String(cur)));\n}\n\nconst reorderedPowerOf2 = (n) => set.has(serialize(String(n)));\n```\n\n## SOLUTION 4\n\nHere we use a more efficient serialization approach.\nSince the range is `[1, 10 ** 9]`, so for every number, the occurrences must small than 10, which means we could use a number to represent the serialization value. For example, `4510` means there are 4 number 4, 5 number 3, 1 number 2 and 0 number 1.\n\n```js\nconst set = new Set();\n\nconst serialize = num => {\n  let val = 0;\n  while (num >= 1) {\n    val += 1 << num % 10;\n    num /= 10;\n  }\n  return val;\n};\n\nfor (let cur = 1; cur < 10 ** 9; cur <<= 1) {\n  set.add(serialize(cur));\n}\n\nconst reorderedPowerOf2 = (n) => set.has(serialize(n));\n```
['JavaScript']