[LeetCode] Reordered Power of 2

869. Reordered Power of 2

Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.

Return true if and only if we can do this in a way such that the resulting number is a power of 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
bool reorderedPowerOf2(int N) {
map<int, set<map<int, int>>> powOfTwo;
for(long start = 1; start <= INT_MAX; start<<=1) {
int len = 0;
long num = start;
map<int, int> numMap;
while(num) {
numMap[num % 10]++;
len++;
num /= 10;
}
powOfTwo[len].insert(numMap);
}

int len = 0;
map<int, int> m;
while(N) {
m[N % 10]++;
len++;
N /= 10;
}

return powOfTwo[len].count(m);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/03/21/PS/LeetCode/reordered-power-of-2/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.