Skip to main content

Maximum Bitwise XOR After Rearrangement - Solution & Explanation

Practice this problem

Problem Statement

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.

 

Example 1:

Input: s = "101", t = "011"

Output: "110"

Explanation:

  • One optimal rearrangement of t is "011".
  • The bitwise XOR of s and rearranged t is "101" XOR "011" = "110", which is the maximum possible.

Example 2:

Input: s = "0110", t = "1110"

Output: "1101"

Explanation:

  • One optimal rearrangement of t is "1011".
  • The bitwise XOR of s and rearranged t is "0110" XOR "1011" = "1101", which is the maximum possible.

Example 3:

Input: s = "0101", t = "1001"

Output: "1111"

Explanation:

  • One optimal rearrangement of t is "1010".
  • The bitwise XOR of s and rearranged t is "0101" XOR "1010" = "1111", which is the maximum possible.

 

Constraints:

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

Approach Overview

Problem Overview: You are given binary strings and allowed to rearrange their characters before computing the bitwise XOR. The goal is to maximize the resulting XOR value. Since XOR produces 1 when two bits differ, the optimal strategy focuses on creating as many mismatched bit positions as possible.

Approach 1: Brute Force Rearrangement (Factorial Time, Exponential Space)

The naive idea tries every possible permutation of the characters in one or both strings, computes the XOR result for each arrangement, and keeps the maximum value. For a string of length n, there are n! permutations, so the total work grows extremely fast. Time complexity is O(n! * n) because each permutation requires an XOR comparison across all positions, and space complexity is O(n!) if permutations are stored. This approach mainly helps build intuition about how rearrangement affects XOR but is impractical even for moderate input sizes.

Approach 2: Greedy Bit Counting (O(n) time, O(1) space)

The XOR operation returns 1 when bits differ (0 ^ 1 or 1 ^ 0). Because you can rearrange characters freely, the exact positions do not matter—only the counts of 0s and 1s matter. Count how many 1s and 0s appear in each string. Then greedily pair opposite bits to maximize mismatches: match 1s from one string with 0s from the other, and vice versa. The number of XOR 1s becomes min(count1A, count0B) + min(count0A, count1B). Iterate once through the strings to collect counts, then compute this formula. Time complexity is O(n) and space complexity is O(1).

This greedy reasoning works because rearrangement removes positional constraints. Instead of worrying about exact placements, you only maximize the number of differing pairs. Problems involving maximizing XOR often reduce to bit-level counting or greedy pairing, especially when reordering is allowed.

Understanding this pattern is useful in problems involving greedy algorithms, bit manipulation, and binary string processing. Many interview questions follow the same principle: transform positional constraints into frequency counts when rearrangement is permitted.

Recommended for interviews: The greedy counting approach is what interviewers expect. Mentioning the brute force permutation idea shows you understand the search space, but deriving the frequency-based formula demonstrates algorithmic insight and leads to the optimal O(n) solution.

Solution

We use an array cnt of length 2 to count the number of character '0' and character '1' in string t.

Then we iterate through string s. For each character s[i], we want to find a character in string t that is different from s[i] to perform the XOR operation, in order to get a larger result. If we find such a character, we set the i-th bit of the answer to '1' and decrement the count of that character by one; otherwise, we can only use a character that is the same as s[i] for the XOR operation, the i-th bit of the answer remains '0', and we decrement the count of that character by one. Finally, we return the answer.

The time complexity is O(n) and the space complexity is O(n), where n is the length of string s.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationsO(n! * n)O(n!)Conceptual understanding of rearrangement impact; not practical for real constraints
Greedy Bit CountingO(n)O(1)Best general solution when characters can be rearranged freely

Video Solution

Maximum Bitwise XOR After Rearrangement | LeetCode 3849 | Greedy Bit Construction • Sanyam IIT Guwahati • 411 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Maximum Bitwise XOR After Rearrangement easy or hard?
Maximum Bitwise XOR After Rearrangement is generally considered a medium-level problem. The challenge is recognizing that rearrangement removes positional constraints and reduces the problem to frequency counting. Once the XOR property is identified, the optimal greedy solution becomes straightforward.
Maximum Bitwise XOR After Rearrangement Python/Java solution
Implement the greedy strategy by counting 0s and 1s in both strings, then computing min(count1A, count0B) + min(count0A, count1B). The result represents the maximum number of positions producing XOR = 1. The same logic works identically in Python, Java, C++, Go, and TypeScript.
How to solve Maximum Bitwise XOR After Rearrangement in O(n)?
Traverse the strings once and count how many 1s and 0s appear in each. Since rearrangement is allowed, pair opposite bits greedily to create the maximum number of XOR 1 results. Compute min(count1A, count0B) + min(count0A, count1B). This avoids permutations and produces the maximum XOR configuration in linear time.
What is the best approach for Maximum Bitwise XOR After Rearrangement?
The greedy bit-counting approach is optimal. Count the number of 1s and 0s in both binary strings, then maximize mismatched pairs because XOR becomes 1 when bits differ. The number of XOR 1s equals min(count1A, count0B) + min(count0A, count1B). This runs in O(n) time with O(1) extra space.
Is Maximum Bitwise XOR After Rearrangement asked at Google/Amazon/Meta?
Problems involving maximizing XOR with greedy bit counting frequently appear in interviews at companies like Amazon, Google, and Meta. Variants often involve binary strings, bit masks, or maximizing differing bits. Interviewers expect candidates to recognize the XOR property and reduce the problem to counting.
What data structure is used in Maximum Bitwise XOR After Rearrangement?
The optimal solution does not require complex data structures. Simple integer counters track the number of zeros and ones in each string. The logic relies on properties of XOR and greedy pairing rather than maps, trees, or heaps.
What is the time complexity of Maximum Bitwise XOR After Rearrangement?
The optimal greedy solution runs in O(n) time because you only scan the strings once to count zeros and ones. After counting, a constant-time formula computes the maximum mismatched pairs. Space complexity remains O(1) since only a few counters are required.

Ready to solve this problem?

Practice Maximum Bitwise XOR After Rearrangement with our built-in code editor and test cases.

Practice on FleetCode