{"id":8940,"date":"2021-11-29T08:43:58","date_gmt":"2021-11-29T16:43:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8940"},"modified":"2021-11-29T08:46:12","modified_gmt":"2021-11-29T16:46:12","slug":"leetcode-76-minimum-window-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-76-minimum-window-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 76. Minimum Window Substring"},"content":{"rendered":"\n<p>Given two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;of lengths&nbsp;<code>m<\/code>&nbsp;and&nbsp;<code>n<\/code>&nbsp;respectively, return&nbsp;<em>the&nbsp;<strong>minimum window substring<\/strong>&nbsp;of&nbsp;<\/em><code>s<\/code><em>&nbsp;such that every character in&nbsp;<\/em><code>t<\/code><em>&nbsp;(<strong>including duplicates<\/strong>) is included in the window. If there is no such substring<\/em><em>, return the empty string&nbsp;<\/em><code>\"\"<\/code><em>.<\/em><\/p>\n\n\n\n<p>The testcases will be generated such that the answer is&nbsp;<strong>unique<\/strong>.<\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within the string.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"ADOBECODEBANC\", t = \"ABC\"\n<strong>Output:<\/strong> \"BANC\"\n<strong>Explanation:<\/strong> The minimum window substring \"BANC\" includes 'A', 'B', and 'C' from string t.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"a\", t = \"a\"\n<strong>Output:<\/strong> \"a\"\n<strong>Explanation:<\/strong> The entire string s is the minimum window.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"a\", t = \"aa\"\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong> Both 'a's from t must be included in the window.\nSince the largest window of s only has one 'a', return empty string.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == s.length<\/code><\/li><li><code>n == t.length<\/code><\/li><li><code>1 &lt;= m, n&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;consist of uppercase and lowercase English letters.<\/li><\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Could you find an algorithm that runs in&nbsp;<code>O(m + n)<\/code>&nbsp;time?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable + Two Pointers<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to store the freq of characters we need to match for t.<\/p>\n\n\n\n<p>Use (i, j) to track a subarray that contains all the chars in t.<\/p>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(m)<\/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  string minWindow(string s, string t) {\n    const int n = s.length();\n    const int m = t.length();\n    vector<int> freq(128);\n    for (char c : t) ++freq[c];\n    int start = 0;\n    int l = INT_MAX;    \n    for (int i = 0, j = 0, left = m; j < n; ++j) {\n      if (--freq[s[j]] >= 0) --left;\n      while (left == 0) {\n        if (j - i + 1 < l) {\n          l = j - i + 1;\n          start = i;\n        }\n        if (++freq[s[i++]] == 1) ++left;\n      }\n    }\n    return l == INT_MAX ? \"\" : s.substr(start, l);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two strings&nbsp;s&nbsp;and&nbsp;t&nbsp;of lengths&nbsp;m&nbsp;and&nbsp;n&nbsp;respectively, return&nbsp;the&nbsp;minimum window substring&nbsp;of&nbsp;s&nbsp;such that every character in&nbsp;t&nbsp;(including duplicates) is included in the window. If there is no such substring, return the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[217,82,41,175],"class_list":["post-8940","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-hard","tag-hashtable","tag-subarray","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8940","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=8940"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8940\/revisions"}],"predecessor-version":[{"id":8942,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8940\/revisions\/8942"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}