Substring XOR Queries - Problem
Substring XOR Queries - Find the shortest binary substring that satisfies XOR conditions

You're given a binary string s and an array of queries. Each query contains two integers [first, second].

For each query, you need to find the shortest substring of s whose decimal value, when XORed with first, equals second. In mathematical terms: substring_value ^ first == second.

Goal: Return the start and end indices [left, right] of the shortest valid substring for each query. If no such substring exists, return [-1, -1]. When multiple substrings have the same length, choose the one with the smallest starting index.

Example: If s = "101101" and query is [4, 5], we need to find a substring whose decimal value XORed with 4 gives 5. Since 4 ^ 5 = 1, we're looking for a substring with decimal value 1, which is "1" at index 0.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "101101", queries = [[4,5]]
โ€บ Output: [[0,0]]
๐Ÿ’ก Note: For query [4,5], we need to find substring whose value XORed with 4 equals 5. Since 4 ^ 5 = 1, we need substring with decimal value 1. The substring "1" at index [0,0] has decimal value 1.
example_2.py โ€” Multiple Queries
$ Input: s = "101101", queries = [[0,5],[1,2]]
โ€บ Output: [[0,2],[2,3]]
๐Ÿ’ก Note: For [0,5]: target = 0^5 = 5, found "101" at [0,2]. For [1,2]: target = 1^2 = 3, found "11" at [2,3].
example_3.py โ€” No Solution
$ Input: s = "101", queries = [[5,1]]
โ€บ Output: [[-1,-1]]
๐Ÿ’ก Note: For query [5,1], target = 5^1 = 4. No substring in "101" has decimal value 4, so return [-1,-1].

Visualization

Tap to expand
๐Ÿ” Secret Code Decoder Visualization๐Ÿ“š Code BookBinary: 101101101101Scanning all sequences..."1"โ†’1, "10"โ†’2, "101"โ†’5...๐Ÿ—‚๏ธ Index CreatedValue โ†’ Position Map:0 โ†’ [1,1]1 โ†’ [0,0] โญ shortest2 โ†’ [0,1]3 โ†’ [2,3]5 โ†’ [0,2]๐Ÿ” Query ProcessingCipher Request: [4, 5]Step 1: Calculate targettarget = 4 โŠ• 5 = 1Step 2: Index lookupindex[1] = [0,0] โœ“Result: [0,0]โšก Processing Multiple Queries EfficientlyQuery [4,5]target = 4 โŠ• 5 = 1lookup: index[1] = [0,0]Time: O(1)Query [0,5]target = 0 โŠ• 5 = 5lookup: index[5] = [0,2]Time: O(1)Query [1,10]target = 1 โŠ• 10 = 11lookup: index[11] = nullTime: O(1), Result: [-1,-1]Total ComplexityPreprocessing: O(nยฒ)Per Query: O(1)๐ŸŽฏ Key Insight: XOR inverse property allows direct target calculation!
Understanding the Visualization
1
Index Building
Scan through the binary code book and catalog every possible sequence with its shortest occurrence
2
Query Processing
For each decoding request, calculate what code value would produce the desired result
3
Instant Lookup
Use your pre-built index to instantly find the shortest sequence with the target value
4
Result Delivery
Return the position of the shortest matching sequence, or indicate if impossible
Key Takeaway
๐ŸŽฏ Key Insight: By preprocessing all substring values into a hash map and using XOR's inverse property (A ^ B = C โŸน A ^ C = B), we can answer each query in O(1) time after O(nยฒ) preprocessing!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ + q)

O(nยฒ) to preprocess all substrings and build hash map, then O(1) per query for q queries

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

Hash map can store up to O(nยฒ) different substring values in worst case

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 104
  • s consists only of '0' and '1'
  • 1 โ‰ค queries.length โ‰ค 105
  • 0 โ‰ค firsti, secondi โ‰ค 109
  • Important: Substring values can be very large, handle overflow carefully
Asked in
Google 15 Meta 8 Amazon 6 Microsoft 4
28.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen