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:
t is "011".s and rearranged t is "101" XOR "011" = "110", which is the maximum possible.Example 2:
Input: s = "0110", t = "1110"
Output: "1101"
Explanation:
t is "1011".s and rearranged t is "0110" XOR "1011" = "1101", which is the maximum possible.Example 3:
Input: s = "0101", t = "1001"
Output: "1111"
Explanation:
t is "1010".s and rearranged t is "0101" XOR "1010" = "1111", which is the maximum possible.
Constraints:
1 <= n == s.length == t.length <= 2 * 105s[i] and t[i] are either '0' or '1'.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.
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.
Python
Java
C++
Go
TypeScript
| 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 |
Maximum Bitwise XOR After Rearrangement | LeetCode 3849 | Greedy Bit Construction • Sanyam IIT Guwahati • 315 views views
Watch 9 more video solutions →Practice Maximum Bitwise XOR After Rearrangement with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor