{"id":5694,"date":"2019-10-02T09:14:48","date_gmt":"2019-10-02T16:14:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5694"},"modified":"2019-10-02T09:15:16","modified_gmt":"2019-10-02T16:15:16","slug":"leetcode-49-group-anagrams","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-49-group-anagrams\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 49. Group Anagrams"},"content":{"rendered":"\n<p>Given an array of strings, group anagrams together.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> <code>[\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]<\/code>,\n<strong>Output:<\/strong>\n[\n  [\"ate\",\"eat\",\"tea\"],\n  [\"nat\",\"tan\"],\n  [\"bat\"]\n]<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All inputs will be in lowercase.<\/li><li>The order of your output does not&nbsp;matter.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>The sorted word will be the key of each group<\/p>\n\n\n\n<p>Time complexity: O(sum(l*log(l)))<br>Space complexity: O(sum(l))<\/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<string>> groupAnagrams(vector<string>& strs) {\n    vector<vector<string>> ans;\n    unordered_map<string, vector<int>> m;\n\n    for (int i = 0; i < strs.size(); ++i) {\n      string c = strs[i];\n      sort(begin(c), end(c));\n      m[c].push_back(i);\n    }\n\n    for (const auto&#038; kv : m) {\n      ans.push_back({});\n      for (int i : kv.second)\n        ans.back().push_back(strs[i]);    \n    }\n\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of strings, group anagrams together. Example: Input: [&#8220;eat&#8221;, &#8220;tea&#8221;, &#8220;tan&#8221;, &#8220;ate&#8221;, &#8220;nat&#8221;, &#8220;bat&#8221;], Output: [ [&#8220;ate&#8221;,&#8221;eat&#8221;,&#8221;tea&#8221;], [&#8220;nat&#8221;,&#8221;tan&#8221;], [&#8220;bat&#8221;] ] Note: All inputs&#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":[304,82,177,4],"class_list":["post-5694","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-anagram","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5694","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=5694"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5694\/revisions"}],"predecessor-version":[{"id":5697,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5694\/revisions\/5697"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}