Class Solution

java.lang.Object
g2501_2600.s2564_substring_xor_queries.Solution

public class Solution extends Object
2564 - Substring XOR Queries.<p>Medium</p> <p>You are given a <strong>binary string</strong> <code>s</code>, and a <strong>2D</strong> integer array <code>queries</code> where <code>queries[i] = [first<sub>i</sub>, second<sub>i</sub>]</code>.</p> <p>For the <code>i<sup>th</sup></code> query, find the <strong>shortest substring</strong> of <code>s</code> whose <strong>decimal value</strong> , <code>val</code>, yields <code>second<sub>i</sub></code> when <strong>bitwise XORed</strong> with <code>first<sub>i</sub></code>. In other words, <code>val ^ first<sub>i</sub> == second<sub>i</sub></code>.</p> <p>The answer to the <code>i<sup>th</sup></code> query is the endpoints ( <strong>0-indexed</strong> ) of the substring <code>[left<sub>i</sub>, right<sub>i</sub>]</code> or <code>[-1, -1]</code> if no such substring exists. If there are multiple answers, choose the one with the <strong>minimum</strong> <code>left<sub>i</sub></code>.</p> <p><em>Return an array</em> <code>ans</code> <em>where</em> <code>ans[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p> <p>A <strong>substring</strong> is a contiguous non-empty sequence of characters within a string.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;101101&rdquo;, queries = [[0,5],[1,2]]</p> <p><strong>Output:</strong> [[0,2],[2,3]]</p> <p><strong>Explanation:</strong> For the first query the substring in range <code>[0,2]</code> is <strong>&ldquo;101&rdquo;</strong> which has a decimal value of **<code>5</code> ** , and **<code>5 ^ 0 = 5</code> ** , hence the answer to the first query is <code>[0,2]</code>. In the second query, the substring in range <code>[2,3]</code> is **&ldquo;11&rdquo;, ** and has a decimal value of <strong>3</strong> , and **3 <code>^ 1 = 2</code> **. So, <code>[2,3]</code> is returned for the second query.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;0101&rdquo;, queries = [[12,8]]</p> <p><strong>Output:</strong> <a href="-1,-1">-1,-1</a></p> <p><strong>Explanation:</strong> In this example there is no substring that answers the query, hence <code>[-1,-1] is returned</code>.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;1&rdquo;, queries = [[4,5]]</p> <p><strong>Output:</strong> <a href="0,0">0,0</a></p> <p><strong>Explanation:</strong> For this example, the substring in range <code>[0,0]</code> has a decimal value of **<code>1</code> ** , and **<code>1 ^ 4 = 5</code> **. So, the answer is <code>[0,0]</code>.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>4</sup></code></li> <li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> <li><code>1 <= queries.length <= 10<sup>5</sup></code></li> <li><code>0 <= first<sub>i</sub>, second<sub>i</sub> <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • substringXorQueries

      public int[][] substringXorQueries(String s, int[][] queries)