{"id":2957,"date":"2018-06-28T22:13:00","date_gmt":"2018-06-29T05:13:00","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2957"},"modified":"2018-06-29T00:25:50","modified_gmt":"2018-06-29T07:25:50","slug":"leetcode-567-permutation-in-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-567-permutation-in-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 567. Permutation in String"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 567. Permutation in String  - \u5237\u9898\u627e\u5de5\u4f5c EP200\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/wpq03MmEHIM?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\u7ed9\u4f60s1, s2\uff0c\u95ee\u4f60s2\u7684\u5b50\u4e32\u4e2d\u662f\u5426\u5b58\u5728s1\u7684\u4e00\u4e2a\u6392\u5217\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/permutation-in-string\/description\/\">https:\/\/leetcode.com\/problems\/permutation-in-string\/description\/<\/a><\/p>\n<p>Given two strings\u00a0<b>s1<\/b>\u00a0and\u00a0<b>s2<\/b>, write a function to return true if\u00a0<b>s2<\/b>\u00a0contains the permutation of\u00a0<b>s1<\/b>. In other words, one of the first string&#8217;s permutations is the\u00a0<b>substring<\/b>\u00a0of the second string.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>s1 = \"ab\" s2 = \"eidbaooo\"\r\n<b>Output:<\/b>True\r\n<b>Explanation:<\/b> s2 contains one permutation of s1 (\"ba\").\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>s1= \"ab\" s2 = \"eidboaoo\"\r\n<b>Output:<\/b> False\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The input strings only contain lower case letters.<\/li>\n<li>The length of both given strings is in range [1, 10,000].<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2962\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/567-ep200.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/567-ep200.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/567-ep200-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/567-ep200-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution: Sliding Window<\/strong><\/h1>\n<p>Time Complexity: O(l1 + l2 * 26) = O(l1 + l2)<\/p>\n<p>Space Complexity: O(26 * 2) = O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  bool checkInclusion(string s1, string s2) {\r\n    int l1 = s1.length();\r\n    int l2 = s2.length();    \r\n    vector&lt;int&gt; c1(26);\r\n    vector&lt;int&gt; c2(26);\r\n    for (const char c : s1)\r\n      ++c1[c - 'a'];\r\n    for (int i = 0; i &lt; l2; ++i) {\r\n      if (i &gt;= l1)\r\n        --c2[s2[i - l1] - 'a'];\r\n      ++c2[s2[i] - 'a'];      \r\n      if (c1 == c2) return true;      \r\n    }\r\n    return false;\r\n  }\r\n};<\/pre>\n<h1>Related Problems<\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/difficulty\/hard\/leetcode-480-sliding-window-median\/\">\u82b1\u82b1\u9171 LeetCode 480. Sliding Window Median<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/heap\/leetcode-239-sliding-window-maximum\/\">\u82b1\u82b1\u9171 LeetCode 239. Sliding Window Maximum<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-438-find-all-anagrams-in-a-string\/\">\u82b1\u82b1\u9171 LeetCode 438. Find All Anagrams in a String<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60s1, s2\uff0c\u95ee\u4f60s2\u7684\u5b50\u4e32\u4e2d\u662f\u5426\u5b58\u5728s1\u7684\u4e00\u4e2a\u6392\u5217\u3002 https:\/\/leetcode.com\/problems\/permutation-in-string\/description\/ Given two strings\u00a0s1\u00a0and\u00a0s2, write a function to return true if\u00a0s2\u00a0contains the permutation of\u00a0s1. In other words, one of the first string&#8217;s&#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":[82,315,215,4,314],"class_list":["post-2957","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-hashtable","tag-permuation","tag-sliding-window","tag-string","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2957","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=2957"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2957\/revisions"}],"predecessor-version":[{"id":2963,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2957\/revisions\/2963"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}