{"id":9942,"date":"2023-02-12T08:50:41","date_gmt":"2023-02-12T16:50:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9942"},"modified":"2023-02-12T10:35:00","modified_gmt":"2023-02-12T18:35:00","slug":"leetcode-2564-substring-xor-queries","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2564-substring-xor-queries\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2564.\u00a0Substring XOR Queries"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 2564. Substring XOR Queries - \u5237\u9898\u627e\u5de5\u4f5c EP410\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/UsH8x4Fg4DI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given a&nbsp;<strong>binary string<\/strong>&nbsp;<code>s<\/code>, and a&nbsp;<strong>2D<\/strong>&nbsp;integer array&nbsp;<code>queries<\/code>&nbsp;where&nbsp;<code>queries[i] = [first<sub>i<\/sub>, second<sub>i<\/sub>]<\/code>.<\/p>\n\n\n\n<p>For the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;query, find the&nbsp;<strong>shortest substring<\/strong>&nbsp;of&nbsp;<code>s<\/code>&nbsp;whose&nbsp;<strong>decimal value<\/strong>,&nbsp;<code>val<\/code>, yields&nbsp;<code>second<sub>i<\/sub><\/code>&nbsp;when&nbsp;<strong>bitwise XORed<\/strong>&nbsp;with&nbsp;<code>first<sub>i<\/sub><\/code>. In other words,&nbsp;<code>val ^ first<sub>i<\/sub> == second<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>The answer to the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;query is the endpoints (<strong>0-indexed<\/strong>) of the substring&nbsp;<code>[left<sub>i<\/sub>, right<sub>i<\/sub>]<\/code>&nbsp;or&nbsp;<code>[-1, -1]<\/code>&nbsp;if no such substring exists. If there are multiple answers, choose the one with the&nbsp;<strong>minimum<\/strong>&nbsp;<code>left<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p><em>Return an array<\/em>&nbsp;<code>ans<\/code>&nbsp;<em>where<\/em>&nbsp;<code>ans[i] = [left<sub>i<\/sub>, right<sub>i<\/sub>]<\/code>&nbsp;<em>is the answer to the<\/em>&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;<em>query.<\/em><\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous non-empty sequence of characters within a string.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"101101\", queries = [[0,5],[1,2]]\n<strong>Output:<\/strong> [[0,2],[2,3]]\n<strong>Explanation:<\/strong> For the first query the substring in range <code>[0,2]<\/code> is <strong>\"101\"<\/strong> which has a decimal value of <strong><code>5<\/code><\/strong>, and <strong><code>5 ^ 0 = 5<\/code><\/strong>, 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 <strong>\"11\",<\/strong> and has a decimal value of <strong>3<\/strong>, and <strong>3<code> ^ 1 = 2<\/code><\/strong>.&nbsp;So, <code>[2,3]<\/code> is returned for the second query. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"0101\", queries = [[12,8]]\n<strong>Output:<\/strong> [[-1,-1]]\n<strong>Explanation:<\/strong> In this example there is no substring that answers the query, hence <code>[-1,-1] is returned<\/code>.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1\", queries = [[4,5]]\n<strong>Output:<\/strong> [[0,0]]\n<strong>Explanation:<\/strong> For this example, the substring in range <code>[0,0]<\/code> has a decimal value of <strong><code>1<\/code><\/strong>, and <strong><code>1 ^ 4 = 5<\/code><\/strong>. So, the answer is <code>[0,0]<\/code>.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>s[i]<\/code>&nbsp;is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><li><code>1 &lt;= queries.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= first<sub>i<\/sub>, second<sub>i<\/sub> &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Pre-compute<\/strong><\/h2>\n\n\n\n<p>We can pre-compute all possible substrings<\/p>\n\n\n\n<p>Time complexity: O(n*32 + m)<br>Space complexity: O(n*32)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> substringXorQueries(string s, vector<vector<int>>& queries) {\n    unordered_map<int, vector<int>> m;\n    const int l = s.length();\n    for (int i = 0; i < l; ++i) {\n      if (s[i] == '0') {\n        if (!m.count(0)) m[0] = {i, i};\n        continue;\n      }\n      for (int j = 0, cur = 0; j < 32 &#038;&#038; i + j < l; ++j) {\n        cur = (cur << 1) | (s[i + j] - '0');\n        if (!m.count(cur))\n          m[cur] = {i, i + j};\n      }\n    }\n    vector<vector<int>> ans;    \n    for (const auto& q : queries) {\n      int x = q[0] ^ q[1];\n      if (m.count(x)) \n        ans.push_back(m[x]);\n      else\n        ans.push_back({-1, -1});\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;binary string&nbsp;s, and a&nbsp;2D&nbsp;integer array&nbsp;queries&nbsp;where&nbsp;queries[i] = [firsti, secondi]. For the&nbsp;ith&nbsp;query, find the&nbsp;shortest substring&nbsp;of&nbsp;s&nbsp;whose&nbsp;decimal value,&nbsp;val, yields&nbsp;secondi&nbsp;when&nbsp;bitwise XORed&nbsp;with&nbsp;firsti. In other words,&nbsp;val ^ firsti ==&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,764,4,63],"class_list":["post-9942","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-precompute","tag-string","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9942"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9942\/revisions"}],"predecessor-version":[{"id":9946,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9942\/revisions\/9946"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}