Watch 10 video solutions for Substring XOR Queries, a medium level problem involving Array, Hash Table, String. This walkthrough by Hua Hua has 2,192 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 Overview: You are given a binary string s and several queries. Each query contains two integers first and second. Your task is to find the shortest substring of s whose decimal value val satisfies val XOR first = second. If such a substring exists, return its start and end indices; otherwise return [-1, -1]. The key observation is that the required substring value is simply target = first XOR second.
Approach 1: Brute Force Enumeration (O(n²) time, O(1) space)
The straightforward method checks every possible substring of s. Convert each substring from binary to decimal and store its value temporarily. For each query, compute target = first XOR second and search through all generated substrings to find one with that value. This approach repeatedly scans the string and performs binary-to-integer conversions, which leads to O(n²) substrings and expensive repeated work. While simple to implement and useful for understanding the relationship between substring values and XOR, it becomes too slow for large inputs.
Approach 2: Optimized Sliding Window with XOR Properties (O(n * 30) time, O(n) space)
The optimized solution uses the XOR identity val = first XOR second. Instead of processing each query independently, preprocess the binary string and compute the decimal value of substrings starting at every index. Because the maximum query value fits within about 30 bits, only substrings of length up to 30 need to be evaluated. Iterate through s, extend a sliding window up to 30 characters, and build the integer value incrementally using bit operations. Store the first occurrence of each computed value in a hash table mapping value → substring indices.
When answering queries, compute target = first XOR second in constant time. Perform a hash lookup to see if this value was observed during preprocessing. If it exists, return the stored indices; otherwise return [-1, -1]. This approach avoids recomputation and limits substring length using binary constraints. It combines efficient scanning of the string, integer construction via bit manipulation, and constant‑time hash lookups. The preprocessing takes O(n * 30) time and each query is answered in O(1).
Recommended for interviews: Interviewers typically expect the hash‑map preprocessing strategy. The brute force approach demonstrates that you understand substring enumeration and XOR evaluation, but it does not scale. The optimized method shows stronger problem‑solving skills by exploiting XOR algebra and limiting substring length to the number of relevant bits.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Substring Enumeration | O(n²) | O(1) | Useful for understanding the problem or when the string size is very small. |
| Sliding Window + Hash Map Precomputation | O(n * 30) + O(q) | O(n) | Best general solution. Efficient when handling many queries on the same binary string. |