🚀

Thanksgiving Sale: Use Coupon Code THANKS25 to Get Extra 25% Off.

00DAYS
:
00HOURS
:
00MINUTES
:
00SECONDS

Leetcode Problem 2564. Substring XOR Queries

2564. Substring XOR Queries

Leetcode Solutions

Preprocessing with Sliding Window and HashMap

  1. Initialize a hashmap to store the binary value as the key and a pair of indices (start, end) as the value.
  2. Iterate over the binary string s using a sliding window of size up to 31.
  3. For each window size, calculate the binary value of the substring and store the first occurrence of this value along with its indices in the hashmap.
  4. For each query, calculate the XOR of firsti and secondi to get the target binary value.
  5. Convert the target binary value to a binary string, ensuring to strip leading zeros.
  6. Look up the binary string in the hashmap to find the corresponding substring indices.
  7. If the binary string is found in the hashmap, return the stored indices; otherwise, return [-1, -1].
  8. Repeat steps 4-7 for all queries and return the results.
UML Thumbnail

Brute Force Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...