Advertisement

Substring XOR Queries - LeetCode 2564 Solution

Substring XOR Queries - Complete Solution Guide

Substring XOR Queries is LeetCode problem 2564, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a binary string s , and a 2D integer array queries where queries[i] = [first i , second i ] . For the i th query, find the shortest substring of s whose decimal value , val , yields second i when bitwise XORed with first i . In other words, val ^ first i == second i . The answer to the i th query is the endpoints ( 0-indexed ) of the substring [left i , right i ] or [-1, -1] if no such substring exists. If there are multiple answers, choose the one with the minimum left i . Return

Detailed Explanation

The problem asks us to find, for each query, the shortest substring within a given binary string `s` such that the XOR of its decimal value with the first element of the query equals the second element of the query. The output should be an array of pairs, where each pair represents the start and end indices of the substring, or `[-1, -1]` if no such substring exists. If multiple substrings satisfy the condition, the one with the smallest starting index should be returned. The input consists of a binary string `s` and a 2D array `queries`. The constraints include the length of `s` being up to 10^4, the number of queries being up to 10^5, and the values within the queries being up to 10^9.

Solution Approach

The solution uses a hash table (memo) to store the decimal values of all possible substrings of `s` up to a maximum length of 32, along with their starting and ending indices. The solution iterates through the string `s`, and for each starting position, it computes the decimal values of substrings up to length 32. These decimal values and their corresponding indices are stored in the `memo`. When processing the queries, it calculates the target decimal value using `first ^ second` and checks if this value exists in the `memo`. If it does, it appends the corresponding indices to the answer array; otherwise, it appends `[-1, -1]`.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (memo) to store substring decimal values and their start/end indices.
  2. Step 2: Iterate through the binary string `s` from left to right.
  3. Step 3: If the current character is '0', check if the value '0' exists in the memo. If not, store the start and end index of '0'. Then, continue to the next character
  4. Step 4: If the current character is '1', start building substrings from this index. For each substring up to length 32:
  5. Step 5: Calculate the decimal value of the current substring.
  6. Step 6: If the decimal value is not in the memo, store the start and end indices of the substring in the memo.
  7. Step 7: After processing all substrings, iterate through the queries.
  8. Step 8: For each query, calculate the target decimal value by XORing `first` and `second`.
  9. Step 9: Check if the target value exists in the memo. If yes, append the corresponding indices from the memo to the result array. If not, append `[-1, -1]` to the result array.
  10. Step 10: Return the result array.

Key Insights

  • Insight 1: The target decimal value of the substring is `first ^ second`. Compute this XOR value for each query to find the desired substring value.
  • Insight 2: Since the maximum value for `first` and `second` is 10^9, the maximum decimal value of a substring can also be 10^9, which means the substring's length is at most 31 (since 2^30 < 10^9 < 2^31). This limits the search space for each start index in the string `s`.
  • Insight 3: Using a hash table (or dictionary) to store precomputed substrings and their indices allows for efficient lookup when processing the queries. This avoids repeated calculations for the same substring values.
  • Insight 4: Leading zeros do not contribute to the decimal value, so '0' should be handled separately or skipped to optimize substring length if necessary.

Complexity Analysis

Time Complexity: O(n + q)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, String, Bit Manipulation.

Companies

Asked at: Trilogy.