2564. Substring XOR Queries Medium

@problem@discussion
#Array#Hash Table#String#Bit Manipulation



1/**
2 * [2564] Substring XOR Queries
3 *
4 * You are given a binary string s, and a 2D integer array queries where queries[i] = [firsti, secondi].
5 * For the i^th query, find the shortest substring of s whose decimal value, val, yields secondi when bitwise XORed with firsti. In other words, val ^ firsti == secondi.
6 * The answer to the i^th query is the endpoints (0-indexed) of the substring [lefti, righti] or [-1, -1] if no such substring exists. If there are multiple answers, choose the one with the minimum lefti.
7 * Return an array ans where ans[i] = [lefti, righti] is the answer to the i^th query.
8 * A substring is a contiguous non-empty sequence of characters within a string.
9 *  
10 * <strong class="example">Example 1:
11 * 
12 * Input: s = "101101", queries = [[0,5],[1,2]]
13 * Output: [[0,2],[2,3]]
14 * Explanation: For the first query the substring in range [0,2] is "101" which has a decimal value of 5, and 5 ^ 0 = 5, hence the answer to the first query is [0,2]. In the second query, the substring in range [2,3] is "11", and has a decimal value of 3, and 3 ^ 1 = 2. So, [2,3] is returned for the second query. 
15 * 
16 * <strong class="example">Example 2:
17 * 
18 * Input: s = "0101", queries = [[12,8]]
19 * Output: [[-1,-1]]
20 * Explanation: In this example there is no substring that answers the query, hence [-1,-1] is returned.
21 * 
22 * <strong class="example">Example 3:
23 * 
24 * Input: s = "1", queries = [[4,5]]
25 * Output: [[0,0]]
26 * Explanation: For this example, the substring in range [0,0] has a decimal value of 1, and 1 ^ 4 = 5. So, the answer is [0,0].
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= s.length <= 10^4
32 * 	s[i] is either '0' or '1'.
33 * 	1 <= queries.length <= 10^5
34 * 	0 <= firsti, secondi <= 10^9
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/substring-xor-queries/
40// discuss: https://leetcode.com/problems/substring-xor-queries/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn substring_xor_queries(s: String, queries: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
46        vec![]
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2564() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.