-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path869_Reordered_Power_of_2.cpp
More file actions
51 lines (45 loc) · 1.08 KB
/
869_Reordered_Power_of_2.cpp
File metadata and controls
51 lines (45 loc) · 1.08 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> powerOf2;
void findPowerOf2(){
for(int i=0;i<32;i++){
powerOf2.push_back(1<<i);
}
}
unordered_map<char,int> findFreq(int n){
string k=to_string(n);
unordered_map<char,int> mp;
for(auto& ch:k){
mp[ch]++;
}
return mp;
}
bool check(unordered_map<char,int>& freq,int pow){
unordered_map<char,int> freqPow=findFreq(pow);
for(auto& [ch,cnt]:freq){
if(cnt!=freqPow[ch]){
return false;
}
}
for(auto& [ch,cnt]:freqPow){
if(!freq.count(ch)){
return false;
}
}
return true;
}
bool reorderedPowerOf2(int n) {
if(!powerOf2.size()){
findPowerOf2();
}
unordered_map<char,int> freqOfN=findFreq(n);
for(auto& pow:powerOf2){
if(check(freqOfN,pow)){
return true;
}
}
return false;
}
};