{"id":165,"date":"2017-11-03T21:02:58","date_gmt":"2017-11-03T13:02:58","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=165"},"modified":"2018-11-27T17:03:37","modified_gmt":"2018-11-27T09:03:37","slug":"438-find-all-anagrams-in-a-string","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=165","title":{"rendered":"438. Find All Anagrams in a String"},"content":{"rendered":"<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><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><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<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def findAnagrams(self, s, p):\r\n        &quot;&quot;&quot;\r\n        :type s: str\r\n        :type p: str\r\n        :rtype: List[int]\r\n        &quot;&quot;&quot;\r\n        result = []\r\n        if not s:\r\n            return []\r\n        p_dict = dict()\r\n        for _p in p:\r\n            Solution.add_k_in_dict(_p, p_dict)\r\n        s_dict = dict()\r\n        index = 0\r\n        for _s in s[index: index + len(p)]:\r\n            Solution.add_k_in_dict(_s, s_dict)\r\n        if s_dict == p_dict:\r\n            result.append(index)\r\n        index += 1\r\n        while index+len(p) &lt;= len(s):\r\n            print index+len(p)\r\n            Solution.del_k_from_dict(s[index-1], s_dict)\r\n            Solution.add_k_in_dict(s[index+len(p)-1], s_dict)\r\n            if s_dict == p_dict:\r\n                result.append(index)\r\n            index += 1\r\n        return result\r\n\r\n    @classmethod\r\n    def add_k_in_dict(cls, k, k_dict):\r\n        if k in k_dict:\r\n            k_dict[k] += 1\r\n        else:\r\n            k_dict[k] = 1\r\n\r\n    @classmethod\r\n    def del_k_from_dict(cls, k, k_dict):\r\n        if k in k_dict:\r\n            k_dict[k] -= 1\r\n        if k_dict[k] == 0:\r\n            del k_dict[k]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a string\u00a0s\u00a0and a\u00a0non-empty\u00a0string\u00a0p, find all the<a href=\"https:\/\/www.ccagml.com\/?p=165\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">438. Find All Anagrams in a String<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/165"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}