/** * @param {number} n * @return {boolean} */ var reorderedPowerOf2 = function(n) { let result = []; let path = []; n = n + ''; const power2Table = generatePowerOf2(); let used = [...Array(n).fill(false)]; dfs(used, n); return result.length > 0;

functiondfs(used, n) { if (path.length === n.length) { if (power2Table.includes(path.join('') - 0)) { result.push([...path]); } return; } for (let i = 0; i < n.length; i++) { if (path.length === 0 && n[i] === '0' || used[i]) continue; path.push(n[i]); used[i] = true; dfs(used, n); used[i] = false; path.pop(); } }

functiongeneratePowerOf2() { let table = []; for (let i = 1; i <= 10**9; i = i*2) { table.push(i); } return table; } };