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 array strategy.
You are given a binary string s, and a 2D integer array queries where queries[i] = [firsti, secondi].
For the ith query, find the shortest substring of s whose decimal value, val, yields secondi when bitwise XORed with firsti. In other words, val ^ firsti == secondi.
The answer to the ith query is the endpoints (0-indexed) of the substring [lefti, righti] or [-1, -1] if no such substring exists. If there are multiple answers, choose the one with the minimum lefti.
Return an array ans where ans[i] = [lefti, righti] is the answer to the ith query.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "101101", queries = [[0,5],[1,2]] Output: [[0,2],[2,3]] Explanation: For the first query the substring in range[0,2]is "101" which has a decimal value of5, and5 ^ 0 = 5, hence the answer to the first query is[0,2]. In the second query, the substring in range[2,3]is "11", and has a decimal value of 3, and 3^ 1 = 2. So,[2,3]is returned for the second query.
Example 2:
Input: s = "0101", queries = [[12,8]]
Output: [[-1,-1]]
Explanation: In this example there is no substring that answers the query, hence [-1,-1] is returned.
Example 3:
Input: s = "1", queries = [[4,5]] Output: [[0,0]] Explanation: For this example, the substring in range[0,0]has a decimal value of1, and1 ^ 4 = 5. So, the answer is[0,0].
Constraints:
1 <= s.length <= 104s[i] is either '0' or '1'.1 <= queries.length <= 1050 <= firsti, secondi <= 109Problem summary: You are given a binary string s, and a 2D integer array queries where queries[i] = [firsti, secondi]. For the ith query, find the shortest substring of s whose decimal value, val, yields secondi when bitwise XORed with firsti. In other words, val ^ firsti == secondi. The answer to the ith query is the endpoints (0-indexed) of the substring [lefti, righti] or [-1, -1] if no such substring exists. If there are multiple answers, choose the one with the minimum lefti. Return an array ans where ans[i] = [lefti, righti] is the answer to the ith query. A substring is a contiguous non-empty sequence of characters within a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
"101101" [[0,5],[1,2]]
"0101" [[12,8]]
"1" [[4,5]]
string-matching-in-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2564: Substring XOR Queries
class Solution {
public int[][] substringXorQueries(String s, int[][] queries) {
Map<Integer, int[]> d = new HashMap<>();
int n = s.length();
for (int i = 0; i < n; ++i) {
int x = 0;
for (int j = 0; j < 32 && i + j < n; ++j) {
x = x << 1 | (s.charAt(i + j) - '0');
d.putIfAbsent(x, new int[] {i, i + j});
if (x == 0) {
break;
}
}
}
int m = queries.length;
int[][] ans = new int[m][2];
for (int i = 0; i < m; ++i) {
int first = queries[i][0], second = queries[i][1];
int val = first ^ second;
ans[i] = d.getOrDefault(val, new int[] {-1, -1});
}
return ans;
}
}
// Accepted solution for LeetCode #2564: Substring XOR Queries
func substringXorQueries(s string, queries [][]int) (ans [][]int) {
d := map[int][]int{}
for i := range s {
x := 0
for j := 0; j < 32 && i+j < len(s); j++ {
x = x<<1 | int(s[i+j]-'0')
if _, ok := d[x]; !ok {
d[x] = []int{i, i + j}
}
if x == 0 {
break
}
}
}
for _, q := range queries {
first, second := q[0], q[1]
val := first ^ second
if v, ok := d[val]; ok {
ans = append(ans, v)
} else {
ans = append(ans, []int{-1, -1})
}
}
return
}
# Accepted solution for LeetCode #2564: Substring XOR Queries
class Solution:
def substringXorQueries(self, s: str, queries: List[List[int]]) -> List[List[int]]:
d = {}
n = len(s)
for i in range(n):
x = 0
for j in range(32):
if i + j >= n:
break
x = x << 1 | int(s[i + j])
if x not in d:
d[x] = [i, i + j]
if x == 0:
break
return [d.get(first ^ second, [-1, -1]) for first, second in queries]
// Accepted solution for LeetCode #2564: Substring XOR Queries
// 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 #2564: Substring XOR Queries
// class Solution {
// public int[][] substringXorQueries(String s, int[][] queries) {
// Map<Integer, int[]> d = new HashMap<>();
// int n = s.length();
// for (int i = 0; i < n; ++i) {
// int x = 0;
// for (int j = 0; j < 32 && i + j < n; ++j) {
// x = x << 1 | (s.charAt(i + j) - '0');
// d.putIfAbsent(x, new int[] {i, i + j});
// if (x == 0) {
// break;
// }
// }
// }
// int m = queries.length;
// int[][] ans = new int[m][2];
// for (int i = 0; i < m; ++i) {
// int first = queries[i][0], second = queries[i][1];
// int val = first ^ second;
// ans[i] = d.getOrDefault(val, new int[] {-1, -1});
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2564: Substring XOR Queries
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2564: Substring XOR Queries
// class Solution {
// public int[][] substringXorQueries(String s, int[][] queries) {
// Map<Integer, int[]> d = new HashMap<>();
// int n = s.length();
// for (int i = 0; i < n; ++i) {
// int x = 0;
// for (int j = 0; j < 32 && i + j < n; ++j) {
// x = x << 1 | (s.charAt(i + j) - '0');
// d.putIfAbsent(x, new int[] {i, i + j});
// if (x == 0) {
// break;
// }
// }
// }
// int m = queries.length;
// int[][] ans = new int[m][2];
// for (int i = 0; i < m; ++i) {
// int first = queries[i][0], second = queries[i][1];
// int val = first ^ second;
// ans[i] = d.getOrDefault(val, new int[] {-1, -1});
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.