Maximum Bitwise XOR of a string

You are given two equal-length binary strings, s and t.

You can rearrange the characters of t in any order, but the characters in s must remain in their original positions.

Your goal is to maximize the numeric value of the bitwise XOR between s and your rearranged t.

Return this maximum possible result as a binary string.

Example 1:

Input: s = "111000", t = "110000"

Output: "111110"

Explanation: To maximize the XOR result, we process s from left to right, greedily attempting to pair each character with its opposite from t. The string s has three '1's and three '0's, while t has two '1's and four '0's. For the first three positions where s is '1', we successfully pair them with three '0's from t, yielding '1's in the XOR result. For the remaining three positions where s is '0', we can only pair two of them with the two '1's available in t, yielding two more '1's. The final remaining '0' in s is forced to pair with the last remaining '0' in t, yielding a '0'. One optimal rearranged t is "000110", producing the maximum XOR value "111110".

Example 2:

Input: s = "10011", t = "00110"

Output: "11111"

Explanation: In this scenario, s contains three '1's and two '0's, while t contains the exact opposite: two '1's and three '0's. This perfect complementary ratio means we can form an ideal pairing for every single bit. From left to right, we pair every '1' in s with a '0' from t, and every '0' in s with a '1' from t. Because we have exactly enough '0's and '1's to satisfy the opposite of every bit in s, we generate a complete string of '1's. One optimal rearrangement of t is "01100", yielding the perfect maximum XOR result of "11111".

Now Your Turn!

Pick the correct output for the given input

Input: s = "1010" t = "1111"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n == s.length == t.length <= 2 * 105
  • s[i] and t[i] are either '0' or '1'.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string maximumXor(string s, string t) {
// Write your code here
}
};
Test Case

Input:

S
T