Maximum Bitwise XOR After Rearrangement - Solution & Explanation
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
tis"011". - The bitwise XOR of
sand rearrangedtis"101" XOR "011" = "110", which is the maximum possible.
Example 2:
Input: s = "0110", t = "1110"
Output: "1101"
Explanation:
- One optimal rearrangement of
tis"1011". - The bitwise XOR of
sand rearrangedtis"0110" XOR "1011" = "1101", which is the maximum possible.
Example 3:
Input: s = "0101", t = "1001"
Output: "1111"
Explanation:
- One optimal rearrangement of
tis"1010". - The bitwise XOR of
sand rearrangedtis"0101" XOR "1010" = "1111", which is the maximum possible.
Constraints:
1 <= n == s.length == t.length <= 2 * 105s[i]andt[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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutations | O(n! * n) | O(n!) | Conceptual understanding of rearrangement impact; not practical for real constraints |
| Greedy Bit Counting | O(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 Python/Java solution
How to solve Maximum Bitwise XOR After Rearrangement in O(n)?
What is the best approach for Maximum Bitwise XOR After Rearrangement?
Is Maximum Bitwise XOR After Rearrangement asked at Google/Amazon/Meta?
What data structure is used in Maximum Bitwise XOR After Rearrangement?
What is the time complexity of Maximum Bitwise XOR After Rearrangement?
Ready to solve this problem?
Practice Maximum Bitwise XOR After Rearrangement with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor