{"id":2852,"date":"2018-05-28T07:56:31","date_gmt":"2018-05-28T14:56:31","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2852"},"modified":"2018-05-28T11:46:13","modified_gmt":"2018-05-28T18:46:13","slug":"leetcode-438-find-all-anagrams-in-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-438-find-all-anagrams-in-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 438. Find All Anagrams in a String"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 438. Find All Anagrams in a String - \u5237\u9898\u627e\u5de5\u4f5c EP191\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/86fQQ7rVGxA?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><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u5728s\u4e2d\u627e\u51fa\u6240\u6709p\u7684Anagrams\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/find-all-anagrams-in-a-string\/description\/\">https:\/\/leetcode.com\/problems\/find-all-anagrams-in-a-string\/description\/<\/a><\/p>\n<p>Given a string\u00a0<b>s<\/b>\u00a0and a\u00a0<b>non-empty<\/b>\u00a0string\u00a0<b>p<\/b>, find all the start indices of\u00a0<b>p<\/b>&#8216;s anagrams in\u00a0<b>s<\/b>.<\/p>\n<p>Strings consists of lowercase English letters only and the length of both strings\u00a0<b>s<\/b>\u00a0and\u00a0<b>p<\/b>\u00a0will not be larger than 20,100.<\/p>\n<p>The order of output does not matter.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>\r\ns: \"cbaebabacd\" p: \"abc\"\r\n\r\n<b>Output:<\/b>\r\n[0, 6]\r\n\r\n<b>Explanation:<\/b>\r\nThe substring with start index = 0 is \"cba\", which is an anagram of \"abc\".\r\nThe substring with start index = 6 is \"bac\", which is an anagram of \"abc\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b>\r\ns: \"abab\" p: \"ab\"\r\n\r\n<b>Output:<\/b>\r\n[0, 1, 2]\r\n\r\n<b>Explanation:<\/b>\r\nThe substring with start index = 0 is \"ab\", which is an anagram of \"ab\".\r\nThe substring with start index = 1 is \"ba\", which is an anagram of \"ab\".\r\nThe substring with start index = 2 is \"ab\", which is an anagram of \"ab\".<\/pre>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2856\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/438-ep191.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/438-ep191.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/438-ep191-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/438-ep191-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution: HashTable + Sliding Window<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 35 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; findAnagrams(string s, string p) {\r\n    int n = s.length();\r\n    int l = p.length();\r\n    vector&lt;int&gt; ans;\r\n    vector&lt;int&gt; vp(26, 0);\r\n    vector&lt;int&gt; vs(26, 0);\r\n    for (char c : p) ++vp[c - 'a'];    \r\n    for (int i = 0; i &lt; n; ++i) {\r\n      if (i &gt;= l) --vs[s[i - l] - 'a'];        \r\n      ++vs[s[i] - 'a'];\r\n      if (vs == vp) ans.push_back(i + 1 - l);\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u5728s\u4e2d\u627e\u51fa\u6240\u6709p\u7684Anagrams\u3002 https:\/\/leetcode.com\/problems\/find-all-anagrams-in-a-string\/description\/ Given a string\u00a0s\u00a0and a\u00a0non-empty\u00a0string\u00a0p, find all the start indices of\u00a0p&#8216;s anagrams in\u00a0s. Strings consists of lowercase English letters only and the length&#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,47],"tags":[304,222,303,4],"class_list":["post-2852","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-anagram","tag-easy","tag-sliding","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2852","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=2852"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2852\/revisions"}],"predecessor-version":[{"id":2857,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2852\/revisions\/2857"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}