{"id":1563,"date":"2018-01-08T16:17:16","date_gmt":"2018-01-09T00:17:16","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1563"},"modified":"2018-01-09T08:00:52","modified_gmt":"2018-01-09T16:00:52","slug":"leetcode-760-find-anagram-mappings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-760-find-anagram-mappings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 760. Find Anagram Mappings"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8f93\u51fa\u6570\u7ec4A\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5728\u6570\u7ec4B\u4e2d\u7684\u7d22\u5f15\u3002<\/p>\n<p>Given two lists\u00a0<code>A<\/code>and\u00a0<code>B<\/code>, and\u00a0<code>B<\/code>\u00a0is an anagram of\u00a0<code>A<\/code>.\u00a0<code>B<\/code>\u00a0is an anagram of\u00a0<code>A<\/code>\u00a0means\u00a0<code>B<\/code>\u00a0is made by randomizing the order of the elements in\u00a0<code>A<\/code>.<\/p>\n<p>We want to find an\u00a0<i>index mapping<\/i>\u00a0<code>P<\/code>, from\u00a0<code>A<\/code>\u00a0to\u00a0<code>B<\/code>. A mapping\u00a0<code>P[i] = j<\/code>\u00a0means the\u00a0<code>i<\/code>th element in\u00a0<code>A<\/code>\u00a0appears in\u00a0<code>B<\/code>\u00a0at index\u00a0<code>j<\/code>.<\/p>\n<p>These lists\u00a0<code>A<\/code>\u00a0and\u00a0<code>B<\/code>\u00a0may contain duplicates. If there are multiple answers, output any of them.<\/p>\n<p>For example, given<\/p>\n<pre>A = [12, 28, 46, 32, 50]\r\nB = [50, 12, 32, 46, 28]\r\n<\/pre>\n<p>We should return<\/p>\n<pre>[1, 4, 3, 2, 0]\r\n<\/pre>\n<p>as\u00a0<code>P[0] = 1<\/code>\u00a0because the\u00a0<code>0<\/code>th element of\u00a0<code>A<\/code>\u00a0appears at\u00a0<code>B[1]<\/code>, and\u00a0<code>P[1] = 4<\/code>\u00a0because the\u00a0<code>1<\/code>st element of\u00a0<code>A<\/code>appears at\u00a0<code>B[4]<\/code>, and so on.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; anagramMappings(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {\r\n        map&lt;int, int&gt; indices;\r\n        for (int i = 0; i &lt; B.size(); ++i)\r\n            indices[B[i]] = i;\r\n        vector&lt;int&gt; ans;\r\n        for (const int a : A)\r\n            ans.push_back(indices[a]);\r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>C++ \/ No duplication<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; anagramMappings(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {\r\n        map&lt;int, vector&lt;int&gt;&gt; indices;\r\n        for (int i = 0; i &lt; B.size(); ++i)\r\n          indices[B[i]].push_back(i);\r\n        vector&lt;int&gt; ans;\r\n        for (const int a : A) {\r\n          ans.push_back(indices[a].back());\r\n          indices[a].pop_back();\r\n        }\r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8f93\u51fa\u6570\u7ec4A\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5728\u6570\u7ec4B\u4e2d\u7684\u7d22\u5f15\u3002 Given two lists\u00a0Aand\u00a0B, and\u00a0B\u00a0is an anagram of\u00a0A.\u00a0B\u00a0is an anagram of\u00a0A\u00a0means\u00a0B\u00a0is made by randomizing the order of the elements in\u00a0A. We want to find an\u00a0index&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,163,70],"tags":[82],"class_list":["post-1563","post","type-post","status-publish","format-standard","hentry","category-array","category-easy","category-hashtable","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1563","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=1563"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1563\/revisions"}],"predecessor-version":[{"id":1584,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1563\/revisions\/1584"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}