-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3849_Maximum-Bitwise-XOR-After-Rearrangement.cpp
More file actions
58 lines (53 loc) · 1.33 KB
/
3849_Maximum-Bitwise-XOR-After-Rearrangement.cpp
File metadata and controls
58 lines (53 loc) · 1.33 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
52
53
54
55
56
57
58
class Solution {
public:
string maximumXor(string s, string t) {
int count[2] = {0,0};
for(char& c : t) count[c-'0'] += 1;
string output;
output.reserve(s.size());
for(char& c : s){
int bit = c - '0';
int complement = 1 - bit;
if(count[complement]){
--count[complement];
output.push_back('1');
}else{
--count[bit];
output.push_back('0');
}
}
return output;
}
};
class Solution {
public:
string maximumXor(string s, string t) {
int count[2] = {0,0};
for(char& c : t) count[c-'0'] += 1;
string output;
output.reserve(s.size());
for(char& c : s){
char res = c;
if(c == '0'){
if(count[1]){
--count[1];
res = '1';
}else{
--count[0];
res = '0';
}
}
if(c == '1'){
if(count[0]){
--count[0];
res = '1';
}else{
--count[1];
res = '0';
}
}
output.push_back(res);
}
return output;
}
};