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 <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks for the shortest substring whose decimal value equals first XOR second. Since each query value is bounded by 10^9, the target value requires at most 30 bits. We can precompute the best substring for each possible decimal value by checking all substrings up to length 30.
Here's how the algorithm would work step-by-step:
def substring_xor_queries(binary_string: str, queries: list[list[int]]) -> list[list[int]]:
best_substrings = {}
string_length = len(binary_string)
# Precompute the shortest occurrences of binary values up to 30 bits.
for length in range(1, 31):
for start_index in range(string_length - length + 1):
end_index = start_index + length - 1
sub_str = binary_string[start_index : end_index + 1]
decimal_val = int(sub_str, 2)
# Keep the shortest substring, or the one with minimum left index for ties.
if decimal_val not in best_substrings:
best_substrings[decimal_val] = [start_index, end_index]
query_results = []
# Resolve each query using the target decimal value.
for first_val, second_val in queries:
target_val = first_val ^ second_val
query_results.append(best_substrings.get(target_val, [-1, -1]))
return query_results
The problem asks us to find the shortest substring whose binary representation represents the decimal value equal to first XOR second. Since the query values are up to 10^9, any relevant decimal value fits within 30 bits. We can precompute the best substring intervals for all unique decimal values appearing in the binary string and quickly answer each query using a hash map.
Here's how the algorithm would work step-by-step:
def substring_xor_queries(binary_string: str, queries: list[list[int]]) -> list[list[int]]:
# Map each decimal value to its best substring range [start, end]
val_to_range = {}
string_length = len(binary_string)
# Precompute all possible values represented by substrings up to length 32
for start_index in range(string_length):
current_val = 0
for length in range(1, min(31, string_length - start_index + 1)):
current_val = (current_val << 1) | int(binary_string[start_index + length - 1])
if current_val not in val_to_range:
val_to_range[current_val] = [start_index, start_index + length - 1]
elif (val_to_range[current_val][1] - val_to_range[current_val][0] + 1) > length:
val_to_range[current_val] = [start_index, start_index + length - 1]
if binary_string[start_index] == '0':
break
# Resolve each query by looking up the target value formed by first ^ second
results = []
for first_val, second_val in queries:
target_val = first_val ^ second_val
results.append(val_to_range.get(target_val, [-1, -1]))
return results
| Case | How to Handle |
|---|---|
| String s consists of all zeros | Valid substrings will evaluate to zero, and the XOR lookup must match the query values accordingly. |
| No valid substring exists for a given query | Return negative one, negative one as the default invalid boundary indicator for that query. |
| Multiple valid substrings match a query | Prioritize and track the shortest length substring by updating boundaries when a smaller valid range is found. |
| Query values are very large integers exceeding standard integer limits | Precompute and store decimal values of substrings up to thirty bits since larger binary values exceed query bounds. |
| String s is very long up to ten to the fifth power characters | Precompute all unique substring decimal values and store their leftmost and rightmost indices in a hash map to ensure efficient O(1) query time. |
| Query first and second values are identical resulting in an XOR target of zero | Search for a substring whose binary representation evaluates to zero, such as a single zero character. |
| Substring has leading zeros like zero zero one | Treat leading zeros carefully since standard integer parsing evaluates it as one unless explicitly handled as a specific substring range. |
| Queries array is empty | Return an empty output array immediately without executing any search logic. |