Tap here to switch tabs
Problem
Submissions

Problem: Substring XOR Queries

med
30 min
Try to solve the Substring XOR Queries problem.

Statement

You are given a binary string s and a two dimensional integer array queries where each queries[i] = [first_i, second_i].

For each query ii, find a substring of s starting at index left_i and ending at index right_i inclusive, such that the decimal value represented by this binary substring, denoted as val, satisfies:

val ⊕\oplus first_i == second_i

where ⊕\oplus represents the bitwise XOR operation.

If multiple valid substrings exist for a query, select the one with the shortest length. If there is still a tie, select the substring with the smallest starting index left_i. If no such substring exists, the answer for that query is [−1,−1][-1, -1].

Return an array ans where ans[i] = [left_i, right_i] represents the answer to the iextthi^{ ext{th}} query, or [−1,−1][-1, -1] if no valid substring is found.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s[i] is either '0' or '1'

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • 0≤0 \leq first_i, second_i ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Substring XOR Queries

med
30 min
Try to solve the Substring XOR Queries problem.

Statement

You are given a binary string s and a two dimensional integer array queries where each queries[i] = [first_i, second_i].

For each query ii, find a substring of s starting at index left_i and ending at index right_i inclusive, such that the decimal value represented by this binary substring, denoted as val, satisfies:

val ⊕\oplus first_i == second_i

where ⊕\oplus represents the bitwise XOR operation.

If multiple valid substrings exist for a query, select the one with the shortest length. If there is still a tie, select the substring with the smallest starting index left_i. If no such substring exists, the answer for that query is [−1,−1][-1, -1].

Return an array ans where ans[i] = [left_i, right_i] represents the answer to the iextthi^{ ext{th}} query, or [−1,−1][-1, -1] if no valid substring is found.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s[i] is either '0' or '1'

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • 0≤0 \leq first_i, second_i ≤109\leq 10^9