{"id":5594,"date":"2019-09-29T00:31:39","date_gmt":"2019-09-29T07:31:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5594"},"modified":"2019-09-29T00:35:11","modified_gmt":"2019-09-29T07:35:11","slug":"leetcode-1208-get-equal-substrings-within-budget","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1208-get-equal-substrings-within-budget\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1208. Get Equal Substrings Within Budget"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;of the same length. You want to change&nbsp;<code>s<\/code>&nbsp;to&nbsp;<code>t<\/code>. Changing the&nbsp;<code>i<\/code>-th character of&nbsp;<code>s<\/code>&nbsp;to&nbsp;<code>i<\/code>-th character of&nbsp;<code>t<\/code>&nbsp;costs&nbsp;<code>|s[i] - t[i]|<\/code>&nbsp;that is, the absolute difference between the ASCII values of the characters.<\/p>\n\n\n\n<p>You are also given an integer&nbsp;<code>maxCost<\/code>.<\/p>\n\n\n\n<p>Return the maximum length of a substring of&nbsp;<code>s<\/code>&nbsp;that can be changed to be the same as the corresponding substring of&nbsp;<code>t<\/code>with a cost less than or equal to&nbsp;<code>maxCost<\/code>.<\/p>\n\n\n\n<p>If there is no substring from&nbsp;<code>s<\/code>&nbsp;that can be changed to its corresponding substring from&nbsp;<code>t<\/code>, return&nbsp;<code>0<\/code>.<\/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 = \"abcd\", t = \"bcdf\", cost = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>\"abc\" of s can change to \"bcd\". That costs 3, so the maximum length is 3.<\/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 = \"abcd\", t = \"cdef\", cost = 3\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>Each charactor in s costs 2 to change to charactor in <code>t, so the maximum length is 1.<\/code>\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 = \"abcd\", t = \"acde\", cost = 0\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>You can't make any change, so the maximum length is 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length, t.length &lt;= 10^5<\/code><\/li><li><code>0 &lt;= maxCost &lt;= 10^6<\/code><\/li><li><code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;only contain lower case English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Binary Search<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/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  int equalSubstring(string s, string t, int maxCost) {\n    const int n = s.length();    \n    vector<int> c(n + 1);\n    auto cit = begin(c) + 1;\n    int ans = 0;\n    for (int i = 0; i < n; ++i, ++cit) {\n      *cit = c[i] + abs(s[i] - t[i]);      \n      int len = cit - lower_bound(begin(c), cit, *cit - maxCost);\n      ans = max(ans, len);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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  int equalSubstring(string s, string t, int maxCost) {\n    const int n = s.length();\n    int ans = 0;    \n    for (int i = 0, j = 0, cost = 0; i < n &#038;&#038; j < n; ++i) {\n      while (j < n) {\n        int c = abs(s[j] - t[j]);\n        if (cost + c > maxCost) break;\n        cost += c;\n        ++j;\n      }\n      ans = max(ans, j - i);\n      cost -= abs(s[i] - t[i]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;s&nbsp;and&nbsp;t&nbsp;of the same length. You want to change&nbsp;s&nbsp;to&nbsp;t. Changing the&nbsp;i-th character of&nbsp;s&nbsp;to&nbsp;i-th character of&nbsp;t&nbsp;costs&nbsp;|s[i] &#8211; t[i]|&nbsp;that is, the absolute difference between&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[52,177,215],"class_list":["post-5594","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-binary-search","tag-medium","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5594","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=5594"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5594\/revisions"}],"predecessor-version":[{"id":5596,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5594\/revisions\/5596"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}