🚀
Thanksgiving Sale: Use Coupon Code
THANKS25
to Get Extra
25% Off
.
00
DAYS
:
00
HOURS
:
00
MINUTES
:
00
SECONDS
Claim Now
✕
Leetcode Problem 2564. Substring XOR Queries
2564. Substring XOR Queries
AI Mock Interview
Leetcode Solutions
Preprocessing with Sliding Window and HashMap
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize a hashmap to store the binary value as the key and a pair of indices (start, end) as the value.
Iterate over the binary string
s
using a sliding window of size up to 31.
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.
For each query, calculate the XOR of
firsti
and
secondi
to get the target binary value.
Convert the target binary value to a binary string, ensuring to strip leading zeros.
Look up the binary string in the hashmap to find the corresponding substring indices.
If the binary string is found in the hashmap, return the stored indices; otherwise, return
[-1, -1]
.
Repeat steps 4-7 for all queries and return the results.
Brute Force Search
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...