3849. Maximum Bitwise XOR After Rearrangement
You are given two binary strings s and t, each of length n.
You may rearrange the characters of t in any order, but s must remain unchanged.
Return a binary string of length n representing the maximum integer value obtainable by taking the bitwise XOR of s and rearranged t.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: string maximumXor(string s, string t) { int op = count(begin(t), end(t), '1'); vector<bool> done(s.length()); for(int i = 0; i < s.length() and op; i++) { if(s[i] == '0') { s[i] = '1'; op--; done[i] = true; } } for(int i = s.length() - 1; i >= 0 and op; i--) { if(done[i]) continue; if(s[i] == '1') { s[i] = '0'; op--; } } return s; } };
|