Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"101" "011"
"0110" "1110"
"0101" "1001"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3849: Maximum Bitwise XOR After Rearrangement
class Solution {
public String maximumXor(String s, String t) {
int[] cnt = new int[2];
for (char c : t.toCharArray()) {
cnt[c - '0']++;
}
char[] ans = new char[s.length()];
for (int i = 0; i < s.length(); i++) {
int x = s.charAt(i) - '0';
if (cnt[x ^ 1] > 0) {
cnt[x ^ 1]--;
ans[i] = '1';
} else {
cnt[x]--;
ans[i] = '0';
}
}
return new String(ans);
}
}
// Accepted solution for LeetCode #3849: Maximum Bitwise XOR After Rearrangement
func maximumXor(s string, t string) string {
cnt := make([]int, 2)
for _, c := range t {
cnt[c-'0']++
}
ans := make([]byte, len(s))
for i := 0; i < len(s); i++ {
x := s[i] - '0'
if cnt[x^1] > 0 {
cnt[x^1]--
ans[i] = '1'
} else {
cnt[x]--
ans[i] = '0'
}
}
return string(ans)
}
# Accepted solution for LeetCode #3849: Maximum Bitwise XOR After Rearrangement
class Solution:
def maximumXor(self, s: str, t: str) -> str:
cnt = [0, 0]
for c in t:
cnt[int(c)] += 1
ans = ['0'] * len(s)
for i, c in enumerate(s):
x = int(c)
if cnt[x ^ 1]:
cnt[x ^ 1] -= 1
ans[i] = '1'
else:
cnt[x] -= 1
return ''.join(ans)
// Accepted solution for LeetCode #3849: Maximum Bitwise XOR After Rearrangement
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3849: Maximum Bitwise XOR After Rearrangement
// class Solution {
// public String maximumXor(String s, String t) {
// int[] cnt = new int[2];
// for (char c : t.toCharArray()) {
// cnt[c - '0']++;
// }
//
// char[] ans = new char[s.length()];
// for (int i = 0; i < s.length(); i++) {
// int x = s.charAt(i) - '0';
// if (cnt[x ^ 1] > 0) {
// cnt[x ^ 1]--;
// ans[i] = '1';
// } else {
// cnt[x]--;
// ans[i] = '0';
// }
// }
//
// return new String(ans);
// }
// }
// Accepted solution for LeetCode #3849: Maximum Bitwise XOR After Rearrangement
function maximumXor(s: string, t: string): string {
const cnt = [0, 0];
for (const c of t) {
cnt[Number(c)]++;
}
const ans: string[] = new Array(s.length).fill('0');
for (let i = 0; i < s.length; i++) {
const x = Number(s[i]);
if (cnt[x ^ 1] > 0) {
cnt[x ^ 1]--;
ans[i] = '1';
} else {
cnt[x]--;
}
}
return ans.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.